I have a loop containing about 6 operations, there is a logic statement that applies to 5/6 of the functions.

1 view (last 30 days)
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.

Answers (2)

Max Heimann
Max Heimann on 27 Jan 2022
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

Rik
Rik on 27 Jan 2022
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!