How to iterate over multiple outputs in a function

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?

답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 6월 28일
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;

카테고리

도움말 센터File Exchange에서 Simulink Functions에 대해 자세히 알아보기

질문:

2023년 6월 28일

답변:

2023년 6월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by