Creating a string for loop.
5 views (last 30 days)
Show older comments
Hi, I'm trying to simplify my code. Right now I have my code set up like this:
Percentageoftimein_mp = sum(in_mp)/(length(in_mp))
Percentageoftimein_mb = sum(in_mb)/(length(in_mb))
Percentageoftimein_mp = sum(in_mp)/(length(in_mp))
Percentageoftimein_mcup = sum(in_mcup)/(length(in_mcup))
Percentageoftimein_mclow = sum(in_mclow)/(length(in_mclow))
Percentageoftimein_bof = sum(in_bof)/(length(in_bof))
Percentageoftimein_mc = PercentageoftimeinMClow+PercentageoftimeinMCup ;
I feel like I should be able to simplify this into a loop then adding my last line after the loop like this:
names ={'in_cob','in_mb','in_mp','in_mcup','in_mclow','in_bof'}
for 1:length(names)
Percentageoftime+names = sum(names)/(length(names))
end
Percentageoftimein_mc = PercentageoftimeinMClow+PercentageoftimeinMCup ;
But I keep getting this error when I try:
% File: plot_smolts.m Line: 91 Column: 9
% Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other
% syntax error. To construct matrices, use brackets instead of parentheses.
Not sure how I should go about doing this.
1 Comment
Stephen23
on 21 Aug 2025
Edited: Stephen23
on 22 Aug 2025
"I'm trying to simplify my code"
Then you need to fix your data design first.
"Not sure how I should go about doing this."
Not with your current approach. Forcing meta-data into variable names is a path you do not want to go down:
All of your operations are exactly the same, so most likely you could place the data in two arrays (eg. table, cell, struct) and then apply that operation to that array. Also note that MATLAB already has a MEAN function, so rather than reinventing the wheel you should use that... if possible with all of the data in just two numeric arrays.
Answers (1)
Matt J
on 21 Aug 2025
Edited: Matt J
on 21 Aug 2025
Instead of separate variables, they should be struct fields,
in.mp=[1,2,3];
in.bof=[11,12,13],
and so forth. Then you can produce a new struct with the percentages simply by doing,
Percentageoftime=structfun(@mean, in, 'uni',0)
5 Comments
Matt J
on 21 Aug 2025
Instead of separate variables,
in_mp=[1,2,3],
in_bof=[11,12,13],
you should have
in.mp=[1,2,3];
in.bof=[11,12,13],
As you can see from the ouput, this packages the mp and bof data into a single structure (struct) variable called 'in'. Since the fields mp, bof, etc... are now held under 'in', you can loop over them and/or operate on them as a group with commands like structfun.
Umar
on 21 Aug 2025
Hi @Sophie,
Since you mentioned you are new to structs, here is a simple way to compute all your percentages and the combined mcusing the struct approach @Matt J described. This keeps everything in one place and avoids multiple separate variables:
% Store your data in a struct in.mp = [1 1 0 1 0]; in.mb = [0 1 1 1 1]; in.cob = [1 0 1 1 0]; in.mcup = [1 0 0 1 1]; in.mclow = [0 1 1 0 1]; in.bof = [1 0 1 0 0];
% Preallocate struct for percentages fields = fieldnames(in); Percentageoftime = struct();
% Compute mean (percentage) for each field
for k = 1:numel(fields)
f = fields{k};
Percentageoftime.(f) = mean(in.(f));
end
% Compute combined MC percentage Percentageoftime.mc = Percentageoftime.mcup + Percentageoftime.mclow;
% Display results disp(Percentageoftime)
Why this works
- in stores all your datasets under descriptive field names, so you don’t need many separate variables.
- mean(in.(f)) calculates the percentage of 1’s in each dataset, which is equivalent to your original sum/length.
- The loop handles all fields automatically, so it’s easy to add more datasets later.
- Combined mc is calculated after all individual percentages, keeping it simple.
Example output:

This should make it easier to understand and implement while keeping your code concise.
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!