I have a loop containing about 6 operations, there is a logic statement that applies to 5/6 of the functions.
2 次查看(过去 30 天)
显示 更早的评论
I have a long loop that has 6 functions, 5 of which adhere to the logic statement. I only need one of the functions to follow it's own statment, and trying to individually input the logic statment for the other 5 has proved difficult.
op==5 is the only one that needs to follow it own statment. i.e not caring about a2 in the input. It should only care about a1, but the statment at the beginning return this operation as 0 when it should be computated. Any help is greatly appreciated.
回答(2 个)
Max Heimann
2022-1-27
Something like this?
function [status, result] = func(op,a1,a2)
status = 0;
result = 0;
[row1,col1] = size(a1)
if row1 ~= col1
return
end
switch op
case 1
...
case 2
...
case 3
...
case 4
...
case 5
[row2,col2] = size(a2)
if row2 ~= col2
return
end
...
case 6
...
end
end
0 个评论
Rik
2022-1-27
You are implementing several functions now. You should consider splitting up your function to several parts. You can use varargin and varargout in your main function and use a switch statement to perform the relevant operation in a separate function.
function [varargout]=func(op,varargin)
%documentation goes here
varargout=cell(1,nargout); % create output array
switch op
case 1
[varargout{:}]=internal_addition(varargin{:})
otherwise
error('operation not implemented')
end
end
function [s,status]=internal_addition(varargin)
status=false;s=[];
try
[a1,a2]=deal(varargin{:});
s=a1+a2;
status=true;
catch
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!