41 views (last 30 days)

Hi, I built a model in simulink, wich is feed with a variable input using this code:

mdl = 'PSDfaulty';

x = [0.8:0.01:0.95];

for i = 1:numel(x)

set_param([mdl '/FrontFault'] , 'Value' , num2str(x(i)));

simout = sim(mdl);

end

and it works perfectly, now I need to save in the workspace the output of the model for each simulation, if possible by modifying the code. Im using a simout block but it only saves the last value, Any advice? Thanks.

Fangjun Jiang
on 31 Jul 2019

In your model, you have a "To Workspace" block logging four signals and writing to a workspace variable "simout". Of course, the workspace variable "simout" is going to be over-written in every for-iteration.

In your script, your also specified a return variable "simout" when calling sim(). This is probably a unintended mistake. Not to mention the name conflict (both are called simout but they are logging different contents). simout=sim() can return values but the contents are dependent on the "Data Import/Export" setting in the model Configuration Parameters. You can't use [out1, out2, out3, out4] = sim(mdl) to expect to return those four signals that you want to log.

I suggest the following and you can take a look at one cell of "Loggings"

mdl = 'PSDfaulty';

x = [0.8:0.01:0.95];

for i = 1:numel(x)

set_param([mdl '/FrontFault'] , 'Value' , num2str(x(i)));

sim(mdl);

Loggings{i}=simout; % simout is the variable name specified in the "To Workspace" block

end

Fangjun Jiang
on 31 Jul 2019

No. You need to keep the "To Workspace" block but not to return variable in sim() call as indicated in my answer. You got 16 cells because "simout" already exists in workspace but all cells are the same.

To acchieve variable name as acc1, acc2, ... (not recommended), you could add a set_param() line in your for-loop to change the variable name of the "To Workspace" block. Then you just need to run the loop with sim(), no need to copy to annother variable e.g. Loggings.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_729993

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_729993

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_730010

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_730010

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_730046

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_730046

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_730354

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_730354

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_730358

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_730358

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_730387

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/474182-save-an-output-each-simulation#comment_730387

Sign in to comment.