Save output of parsim to file
Show older comments
I would like the output of parsim to be saved to a file rather than to the workspace, as my simulation output is quite large and I need to run many iterations, all of which will occupy inordinate amounts of memory.
Currently, I have the following code:
for ii = 1:number_runs
inObj(ii) = Simulink.SimulationInput(mdl);
inObj(ii) = inObj(ii).setModelParameter('LoggingToFile', 'on');
inObj(ii) = inObj(ii).setModelParameter('LoggingFileName', 'c:\tmp\logfiles\run']);
inObj(ii) = inObj(ii).setVariable('some_variable',randn(1));
end
parsim(inObj, ...
'ShowSimulationManager', 'off', ...
'UseFastRestart','on', ...
'TransferBaseWorkspaceVariables', 'on');
The output of parsim appears in the default workspace variable ans as an array of SimulationOutput objects. I also see a set of files, c:\tmp\logfiles\run_1, etc. which contain simulation metadata, but not the simulation output that I want.
How do I cause the data that appears in ans to be written to a set of files instead?
3 Comments
Paul
on 15 Jul 2026 at 23:49
Hi Hugh,
How is the model set up to log signals?
I tried this with a simple model and observed the following:
Signals logged via ToWorkspace blocks do not show up in the run_*.mat files.
Signals logged via ToWorkspace blocks are stored in the single output from the parsim command, which is ans in your workflow.
Signals logged via "signal logging" that go to logsout by default are saved in the run_*.mat files (I think this will only be true if logging in Dataset format).
Signals logged via "signal logging" that go to logsout by default are also available from ans. However, the logged signals in ans(ii) are "linked" to the corresponding run_ii.mat file. That is, if I delete the run_ii.mat file, the logged data in ans(ii).llogsout is no longer accessible.
Do you see the same behaviors?
Hugh
on 16 Jul 2026 at 15:48
My reading of this doc page Log data to file - Option to log data that uses Dataset format to MAT or MLDATX file - MATLAB is that data logged via To Workspace can't be saved to a .mat file. But it can be saved to an .mldatx file.
I'm not at all familiar with .mldatx. I kind of got it to work, but also got an error message indicating that Simulink was still trying to generate a .mat file, so I probably didn't have all of parameters set correctly.
Or move away from ToWorkspace and use Signal Logging instead, though I admint to being confused about the relationship between the logsout variable in ans and the logsout variable stored in the .mat file. In particular, if each simulation logs 100 MB of data, is that 100 MB physically stored in ans?
Accepted Answer
More Answers (0)
Categories
Find more on Data Logging 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!