Saving signals for each case of parsim() simulation

25 次查看(过去 30 天)
I am writing a script to set parameters for a parsim() simulation. But I want to save signals at each simulation case into a specific file name, so that at the end of the run I will like to see a number of files created for each case. I have gone through the help-matlab but still is not clear for me how to solve my case. Here are specific queries that I would like somebody to clarify them. (Thanks in advance)
1.- How to save signals into a external file (*.mat) for each simulation case when running parasim()?. I have tried the following code but it doesn't work. I don't see any file creted neither in my workspace nor in my project FOLDER
for i = spv_length:-1:1
in(i) = Simulink.SimulationInput('sldemo_househeat');
in(i) = in(i).setBlockParameter('sldemo_househeat/Set Point',...
'Value',num2str(SetPointValues(i)));
%name = strcat('case_number',i,'.mat');
in(i) = in(i).setModelParameter('SimulationMode','accelerator',...
'LoggingToFile','On',...
'LoggingFileName','name',...
'SaveFormat','Dataset');
end
out = parsim(in,'ShowSimulationManager','on','ShowProgress','on');
However, If I select "signal loging" from Configuration Parameter as below
I can Only see my file name inside the structure "out", but this is not what I want. I would like the script to create a file (*.MAT) in my project FOLDER for each test case
It seems some progress here, but how to do this PROGRAMMATICALLY (from script)?
Regarding naming the output file, I will need to rename the file for each case that parsim() runs, otherwise the last case will override the previous one. Here is my attempt (doesn't work) :
for i = spv_length:-1:1
in(i) = Simulink.SimulationInput('sldemo_househeat');
in(i) = in(i).setBlockParameter('sldemo_househeat/Set Point',...
'Value',num2str(SetPointValues(i)));
name = strcat('case_number',i,'.mat');
in(i) = in(i).setModelParameter('SimulationMode','accelerator',...
'LoggingToFile','On',...
'LoggingFileName',name,...
'SaveFormat','Dataset');
end
out = parsim(in,'ShowSimulationManager','on','ShowProgress','on');
2.- My Simulink model has already several signals selected as "log Selected Signal" (85 signal toal). But for my parsim() case I want to save ONLY a partial number of signals (15 signals). How to do this? is there a programmatically way to do it?, Do I need to un-log all signals and log only the ones I need?
3.- I have a signal created in a script ("TotalFAConnectedSB"), I move this signal into my Simulink model, log it (selecting ""log Selected Signal") so it can be saved it along the other logged signals, BUT it triggers an error when run parsim(). What Can I do in this case, how can i save a signal that is constant all the time. And also I would like to save a PARAMETERS within the log output so I can Plot it. is this possible?
*.m file code:
TotalFAConnectedSB = C1+C2+C3
where C1,C2,C3 are input parametsr (defined in the same script) to a simulink block.

回答(1 个)

Paul
Paul 2023-8-23
编辑:Paul 2024-7-25
Hi TeraWatt,
1) Not sure what the issue is. I created a simple model with one logged signal. Ran the following script (R2022a):
in = Simulink.SimulationInput('terawatt');
in = in.setModelParameter('LoggingToFile','on','LoggingFileName','foo.mat','SimulationMode','accelerator');
in(2) = in(1);
delete foo*.mat % just in case old files hanging around
out = parsim(in);
After the script runs, here's what I have in my directory (note that the file names automagically were assigned unique names).
>> dir foo*.mat
foo_1.mat foo_2.mat
>> whos -file foo_1.mat
Name Size Bytes Class Attributes
SimulationMetadata 1x1 6400 Simulink.SimulationMetadata
logsout 1x1 1564 Simulink.SimulationData.Dataset
Not sure why this wouldn't work for you.
2) Not sure about this
3) It looks like you should be using the Constant block. The FromWorkspace block is typically used to import an array of data where each row corresponds to an instant in time. Check the corresponding doc pages for details.
  2 个评论
TeraWatt
TeraWatt 2023-8-23
Thanks Paul for your comment. I have tested my code using an example model from MATLAB and found that my code doesn't because the capital letter for "On":
setModelParameter('LoggingToFile','On','LoggingFileName','case_test.mat');
The one that works is:
setModelParameter('LoggingToFile','on','LoggingFileName','case_test.mat');
However, the wrong code doesn't trigger any error message in my PROJECT environment (it only triggers an error when running the example MATLAB model). I think this is another mistery for MATLAB
Paul
Paul 2023-8-23
What's the error message when using the wrong code with the example MATLAB model?
Just to be clear, if you use 'on' then both the example model and your model work correctly with parsim?
Did using the Constant block solve item (3)?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Run Multiple Simulations 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by