Move on to next iteration when condition is satisfied, specifically when using functions
16 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Stephen23
2019 年 10 月 3 日
編集済み: Stephen23
2019 年 10 月 3 日
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)
その他の回答 (1 件)
Adam Danz
2019 年 10 月 2 日
編集済み: Adam Danz
2019 年 10 月 2 日
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 件のコメント
Adam Danz
2019 年 10 月 3 日
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).
参考
カテゴリ
Help Center および File Exchange で Low-Level File I/O についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!