How to stop running the code for conditional logical output?

3 次查看(过去 30 天)
I am writing a function with multiple outputs. The first one being a logical output. I do not need further outputs if logical output is 0. I am wondering what is the best way to stop running the code if logical output is 0? I used this code, it seems like working.
logical_out = ~isempty(subject_in_feeder);
% don't need further analysis if output is 0
if logical_out == 0
return
end
% further analysis
terminal_coordinatetimes = [subject_in_feeder.coordinatetimes(1), ...
subject_in_feeder.coordinatetimes(end)]';

采纳的回答

Voss
Voss 2022-4-1
Using return like that is completely appropriate for what you want to do, but you must make sure all of your function's outputs are defined when the function returns. The second output may not be used in case the first one is false or 0, but the second one has to have a value anyway.
[result1,result2] = my_function([])
result1 = logical
0
result2 = []
function [logical_out,terminal_coordinatetimes] = my_function(subject_in_feeder)
logical_out = ~isempty(subject_in_feeder);
% don't need further analysis if output is 0
if logical_out == 0
% have to return some value for terminal_coordinatetimes
terminal_coordinatetimes = [];
return
end
% further analysis
terminal_coordinatetimes = [subject_in_feeder.coordinatetimes(1), ...
subject_in_feeder.coordinatetimes(end)]';
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by