Creating a string for loop.
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.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. 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. strings, for loop MATLAB Answers — New Questions