How to iterate over multiple outputs in a function
Show older comments
I have a model contained in a Simulink "Matlab Function" block that contains code which looks like this:
function [output1, output2, output3, output4, etc...] = func(input)
How do I iterate over and update the outputs?
If I just assign individually, ie: output1 = 1, I can see that the value was updated as expected.
However, if I create an array and assign all outputs, like this:
outputs = [output1, output2, output3, ouput4, etc...];
Then do: outputs(1) = 1;
I do not see the matlab function block output at output1 update to 1.
How can I successfully loop over and update multiple outputs to a function contained inside this Matlab function block?
Answers (1)
Sulaymon Eshkabilov
on 28 Jun 2023
If understood your question correctly, here is one very simple example how to get it:
INS = randi(25, 1,5); % Randomly generated 5 input values
for ii=1:numel(INS)
IN = INS(ii); % One input per cycle
OUT{ii} = sim("Multiple_OUT.slx") % Recall Simulink model and simulate it per cycle. And keep the sim results in OUT
end
Note that MATLAB fcn has this syntax to give three outputs:
function [y1, y2, y3] = fcn(u)
y1 = u;
y2 = 2*u;
y3 = u^2;
Categories
Find more on Simulink Functions 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!