How to make a matlab script that runs simulink model to run parallel instances of the model

I have a working matlab script to manage the run of a simulink model. Now I would like to transform the script to run several instances of the model (with a different random seed, fors statistical purposes).
Values in Model Block Dialogs are set by using Matlab workspace, i.e., before launching
out = sim(model)
I set all of the needed params in the script.
To translate the script to run several instances in parallel where the only difference is the change of "seed" in the "Colored Noise" block, I proceeded as follow:
simIn(1:Nproc) = Simulink.SimulationInput(model);
for idx = 1:Nproc
Seed = SeedPool(idx);
simIn(idx) = simIn(idx).setVariable('Seed',num2str(Seed));
end
parallel_out = parsim(simIn);
but I got several errors and I don't understand why.
First, it looks like the variables in workspace are not available anymore to the different simIn(idx) (I get "Unrecognized function or variable ..." error). However, if I explicitly set for any simIn(idx) the said variables by, e.g.,
simIn(idx) = simIn(idx).setVariable('Delay',num2str(Delay));
and similar for the other params, I start getting very strange errors like:
  • Mask parameters must be scalar values
  • Invalid setting in ... for parameter ...
  • Matrix dimensions must agree
After searching the doc and the web the whole day, I gave it up.
This is very frusrtating because the script works well if I perform a simulation at a time.
Any suggestion?

 Risposta accettata

Hi alberto,
The third input to setVariable should be the actual value of the variable.
simIn(idx) = simIn(idx).setVariable('Seed',Seed); % assuming the the model variable and Seed are both numerical
If you have several base workspace variables that are common across all runs, you can call parsim using the TransferBaseWorkspaceVariables name/value pair, though that's not the recommended approach for large scale simulation.

2 Commenti

OK, thank you for the comment about the numerical nature of 'Seed', however I see that the real big problem is related to the use of workspace variables.
You suggest using 'TransferBaseWorkspaceVariables' but at the same time you warned me that it is not the recommended approach. Although, as you suggest, it solves all of my problems.
Browsing doc, I found that maybe I should use the Model Workspace. I'm trying to find a way to use it but from the doc I'm not sure how to do it. I found different options using Simulink.data.connect however it seens that it was introduced Since R2024a but (for reason that will be off topic here) I'm forced to use the R2023b.
What options would you recommend? Is there any way to define a "model workspace"? If so, where can I find some clear documentation with working examples?
Thank you for your support.
My warning about TransferBaseWorkspaceVariables was echoing the documentation at the linked page. If you are satisfied with it for your workflow, and you can ensure that the base workspace is in the state you want at the start of the script, then maybe that's all you need.
Documentation on model workspaces: Model Workspaces - MATLAB & Simulink
Adapting the current workflow to use a model workspace depends on how the variables are populated into the base workspace if the first place. Are they stored in a .mat file and loaded? Are they outputs from some other m-function or m-script? Typed by hand? etc.

Accedi per commentare.

Più risposte (0)

Categorie

Richiesto:

il 10 Mar 2026

Modificato:

il 13 Mar 2026

Community Treasure Hunt

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

Start Hunting!

Translated by