Save output of parsim to file

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
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
Hugh on 16 Jul 2026 at 15:48
I haven't investigated all the possibilities you mention above, but I have observed these:
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.
This is the problem that I'd like to solve. I want signals logged via ToWorkspace blocks to show up in the run_*.mat files, not in the parsim output.
Paul
Paul on 16 Jul 2026 at 17:17
Edited: Paul on 16 Jul 2026 at 22:19
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?

Sign in to comment.

 Accepted Answer

Martin Ryba
Martin Ryba on 19 Jul 2026 at 0:55
The key I think is you want to use a presim and/or postsim function to generate your memory-intensive inputs and/or process your simulation outputs to the reduced size that you actually need. I do this often for Monte Carlo Simulink runs on a DUT. The MATLAB example Accelerate BER Measurement has some of the details that gave me the recipe to follow. I guess if you really need to save raw output, you can put a save() call in the postSimFcn; I tend to process what I need in the function and just return the key numbers in a "results" structure that is the output of the function.

More Answers (0)

Tags

Asked:

on 15 Jul 2026 at 18:56

Answered:

on 19 Jul 2026 at 0:55

Community Treasure Hunt

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

Start Hunting!