How to use postSimFcn with parsim and Parallel Toolbox
8 views (last 30 days)
Show older comments
Hi.
I need to execute several Simulink simulations and save their results when the simulation is compleated. I prefer to save the results at each job ending instead to wait all jobs to avoid time waste since the simulation is big (26 ms of simulation at resolution of 80MHz).
I tried a solution with parsim. Here the code:
SimCount = 100;
SimRep = repmat(Simulink.SimulationInput('SimModel'), SimCount, 1);
% Create Simulation object and add all Simulink needed variables
for i = 1:SimCount
SimRep(i) = Simulink.SimulationInput('SimModel');
SimRep(i) = SimRep(i).setVariable('var1', i);
SimRep(i) = SimRep(i).setUserString(num2str(i));
SimRep(i) = SimRep(i).setPostSimFcn(@postSimFunction);
end
out = parsim(SimRep, 'ShowSimulationManager', 'on');
Here my postSimFunction defined in postSimFunction.m file:
function simOut = postSimFunction(simOut)
simNum = str2double(simOut.SimulationMetadata.UserString);
fprintf('Simulation n.%03d completed.\n', simNum);
end
The function is correctly called if I run the simulation with
out = sim(SimRep(1));
but it is not called if I run:
out = parsim(SimRep)
no matter the simulations number or size.
How can I solve the problem?
0 Comments
Answers (1)
Gayathri
on 3 Feb 2025
I understand that you are not able to save the output using the "postSimFunction". But another effective way to save the logged output in a '.mat' file would be to use the 'LoggingToFile' option. Please modify the code as shown below to save the output in different '.mat' files.
for i = 1:SimCount
SimRep(i) = Simulink.SimulationInput('SimModel');
SimRep(i) = SimRep(i).setVariable('var1', i);
SimRep(i) = SimRep(i).setUserString(num2str(i));
name = strcat('out',num2str(i),'.mat');
SimRep(i) = SimRep(i).setModelParameter('LoggingToFile','on',...
'LoggingFileName',name,...
'SaveFormat','Dataset');
end
out = parsim(SimRep, 'ShowSimulationManager', 'on');
Also, please ensure that you have logged the signal in the Simulink model.
For more information on saving the output using 'LoggingToFile', please refer to the below documentation link.
You can also refer to the MATLAB Answer questions below to get a more clear idea on the method.
Hope you find this information helpful!
1 Comment
Gayathri
on 7 Feb 2025
Please provide the complete path to save the file using the "save" function inside the "postSimFunction". Each worker is in a different working directory during execution of parsim, so saving the file without specifying a path will save the file, but we will not see it since it is located in different temporary directories.
Please modify the code as shown below.
function simOut = postSimFunction(simOut)
simNum = str2double(simOut.SimulationMetadata.UserString);
save(sprintf('C:/Users/Desktop/simResult_%03d.mat', simNum), 'simOut'); % Include the full path here
end
The answer provided before can also be used as an alternative method. Hope you find this information helpful!
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!