MATLAB Answers

Move on to next iteration when condition is satisfied, specifically when using functions

27 views (last 30 days)
Z Liang
Z Liang on 2 Oct 2019
Commented: Z Liang on 3 Oct 2019
for i1 = 1:10
[out1,out2] = func1(in1,in2) % in1 is file name for 10 files in directory
end
func1 analyzes some files which can be analyzed, but I want it to skip files which cannot
function [out1,out2] = func1(in1,in2)
if strcmp(in1,'File #1') % check that in1 (input file name) is File #1
% do some analysis
end
if strcmp(in1,'File #2')
% do some analysis
end
if strcmp(in1,'File #3')
continue % I want to skip analysis of File #3 and continue/move on to File #4 in the above for loop
end
% rest of the files
end
the error I get is "A CONTINUE may only be used within a FOR or WHILE loop."
I understand what the error means, but want to ask how I can accomplish the same goal, of continueing a for loop outside the function when a condition is met.
The analyses require some manual identification of parameters, such as window indices, so I have to input them manually each time.

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 3 Oct 2019
Edited: Stephen Cobeldick on 3 Oct 2019
Adding return does not skip to the start of the next loop iteration, it just exits the function.
But you can easily return NaNs
function [height,weight] = body_metric(fileName,data_mat)
switch fileName
case 'Group 1'
data_mat = data_mat(2:20,:) % exclude made up outlier at row 1
case 'Group 2'
height = NaN;
weight = NaN;
return
case 'Group 3'
data_mat = data_mat([1:8,10:20],:) % exclude made up outlier at row 9
otherwise
... ?
end
height = data_mat(:,1); % 1st column are heights
weight = data_mat(:,2); % 2nd column are weights
end
And then simply check for those values within the loop:
for k = 1:10
fileName = filesMat(k);
data_mat = load(fileName); % data_mat is 20x2 matrix
[height,weight] = body_metric(fileName,data_mat);
if isscalar(height)&&isnan(height)&&isscalar(weight)&&isnan(weight)
continue
end
height_mean = mean(height);
weight_mean = mean(weight);
... the rest of your code.
end
Of course you do not need to use NaN, you should pick a class/size/value that will never be returned as valid data, and check for the existence of that class/size/value. E.g. empty arrays are easy to check for:
height = [];
weight = [];
...
if isempty(height)&&isempty(weight)

More Answers (1)

Adam Danz
Adam Danz on 2 Oct 2019
Edited: Adam Danz on 2 Oct 2019
It's better to use elseif in the form below. A 'return' will stop execution of the function and return execution to the caller.
function [out1,out2] = func1(in1,in2)
if strcmp(in1,'File #1') % check that in1 (input file name) is File #1
% do some analysis
elseif strcmp(in1,'File #2')
% do some analysis
elseif strcmp(in1,'File #3')
out1 = NaN; %or whatever the default output is
out2 = NaN; %or whatever the default output is
return % <--------------------------- stop and go back to caller
elseif
%... rest of the files
end
end
*Updated to add default outputs. The default outputs can also be defined prior to the conditionals in order to ensure that they exist prior to returning.

  4 Comments

Show 1 older comment
Z Liang
Z Liang on 3 Oct 2019
Hi there, thanks so much for answering
I tried it but I think I must have implemented it wrong
for i=1:10
fileName = filesMat(i);
data_mat = load(fileName); % data_mat is 20x2 matrix
[height,weight] = body_metric(fileName,data_mat);
height_mean = mean(height);
weight_mean = mean(weight);
end
function [height,weight] = body_metric(fileName,data_mat)
if fileName == 'Group 1'
data_mat = data_mat(2:20,:) % exclude made up outlier at row 1
height = data_mat(:,1); % 1st column are heights
weight = data_mat(:,2); % 2nd column are weights
elseif fileName == 'Group 2'
height = nan;
weight = nan;
return
elseif fileName == 'Group 3'
data_mat = data_mat([1:8,10:20],:) % exclude made up outlier at row 9
height = data_mat(:,1); % 1st column are heights
weight = data_mat(:,2); % 2nd column are weights
end
end
And so these two together did not work.
At first, when the "height = nan; weight = nan;" lines were absent, I did get the error along the lines of output missing or something.
But even with them present, I get a different error along the lines of "function mean cannot take 'nan' as input" or something (sorry don't have matlab on home comp so I'm paraphrasing). This error leads me to believe that despite the "return" in the function, which I was hoping will continue/advance to the next iteration of the parent for loop, it instead does not advance but rather keeps going down the lines on the same iteration of the for loop. I hope I've explained what trouble I'm running into. Of course all this code is made up, the actual code is a few hudred lines long but this here in the middle is where I'm running into trouble. I do have to manually exclude outliers/artifacts for each file, and this is how I thought to do it. If there's a better way I'm all ears!
Adam Danz
Adam Danz on 3 Oct 2019
As Stephen Cobeldick mentioned, the 'return' merely exits the body_metric() funciton. The default "NaN" values I added were just an example. You should define the default values in a smarter way. NaNs could be the best solution but that depends on the expected size/shape of the data and how you're going to process it. Maybe the default should be 0s or empties [ ]. That's up for you to decide. The mean() function can handle empty values as well as NaNs (see "omitnan" option).

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!