Save an output each simulation

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.

6 个评论

simout{i} = sim(mdl) ;
Possibly you might be able to assign to simout(:, i) instead of using a cell array.
That could be a solution, but I forgot to mention that my model has multiple outputs, how could I indicate which variable I want to save. Thanks for answer
mdl = 'PSDfaulty';
x = [0.8:0.01:0.95];
nx = length(x);
simouts = cell(nx, 2); %2 is how many of the outputs you want to save
for i = 1:nx
set_param([mdl '/FrontFault'] , 'Value' , num2str(x(i)));
[out1, out2, out3, out4] = sim(mdl); %use appropriate number
simouts{i, 1} = out2; simouts{i, 2} = out4; %save the ones you want
end
Thanks Walter, I tried your solution, but I am getting the following error.
Number of left-hand side arguments doesn't match block diagram. When specifying that root-level outports are to be returned individually, the number of left-hand side arguments must be 2 (for T,X) plus number of root-level outport blocks.
My model looks as follows.
Assuming the output ports are out of the mask, I don't understand what's wrong. Also I edited the input part of the code to
set_param([mdl '/Faults/FrontFault'] , 'Value' , num2str(x(i)));
so thats not the problem.
Thank you for responding, sorry for the inconvenience.
I am not certain but I suspect that you will need 3 outputs from the sim() command and that all of the interesting values are muxed together in the third output. However at the moment I am not sure what datatype that third output would be or how the values would be arranged for it.
Ah OK I understand. If this can help, the data I am looking for is the acceleration data, I have recorded it manually and it is a double.
Name Size Bytes Class Attributes
acc 6251x1 50008 double
Thanks again.

请先登录,再进行评论。

 采纳的回答

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

3 个评论

Thanks for responding, I have removed the "to workspace block" and used the solution you give me. Now as a result I have an 1x16 cell array called loggings in each column the data I need, e.g the loggins {1,1} is the data for the first run, is not exactly what I wanted but it works. I wanted to have each data separately, that is to say something like acc1, acc2 ... each in a different variable. Thank you for taking the time to respond.
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.
Sorry, but I checked the cells and are not the same, they have different data, and they are the data I need. Only these are more useful to me by having them in different variables. Thanks for responding, it has been very useful.
Edit: actually just typing Loggins{n,n} I can acces to the info and use it as I need. Thanks for the reply.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Save Run-Time Data from Simulation 的更多信息

产品

版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by