Simulink parameters change during simulation from Matlab cmd line

I need to change the parametrs of the running Simulink simulation model from Matlab cmd line - I am not running the Simulink simulation programatically but just clicking on Simulation button (Ctrl+T) to run.
Is there simulation obeject available in workspace to set variable for this simulation?

 Accepted Answer

To be clear, the intent is to change at the command line, while the simulation is running, base workspace variables that are block parameters?
If so, check the relevant section at Tune and Experiment with Block Parameters. Other sections of that page might be of interest as well. In short, I think you change the value of the variable at the command line and then Update the model.
If you don't mind me asking, what is the use case?

7 Comments

I need to change the variable at command line which are defined in ase workspace I need to change the block parametrs values during the simulation run.
1) Opening in model data editor does not effectively works for since I have a complex model and to see the block variable and its value to change then I need to seartch appripriate sublock, and highlith to see it in model editor.
2) Changing the varable value from matlab command line directly - it does not have effect on simulation during runtime
3) changing variable from within Model Data Editor to real values it rewrites the original varable configuration
there is multiple variables, constant inputs to the model in various sublock so model data editor seems not practical therefore I want set those varables, constant inputs values via matlab command
So intention is to change variables' values and constant inputs (constant simulink block) during runtime.
RTFM it's quite none effective and very little help on use case :(
I RTFM and see this line:
"During the simulation, you must update the model diagram after you change the value of a workspace variable. To update the model diagram, on the Simulink Toolstrip, on the Modeling tab, in the Compile section, click Update Model."
which is basically what I said:
"change the value of the variable at the command line and then Update the model."
When I followed those instructions it worked for me.
Is it possible to "Update model" programatically in runtime ?
The link to the subsystem is quite complicated through variant sustem - how can I refer to the variable witrhin variant system?
"Is it possible ....."
"... how can I refer ..."
I don't understand the need to refer to the variable within the variant subsystem. I thought the block parameter in question is defined by a variable in the base workspace (and not some other workspace, e.g. the model workspace).
Note that not every block parameter is tunable during simulation. See the doc page link to Tune and Experiment with Block Parameter Values in the original Answer for further discussion and how to determine if a particular parameter is tunable during simulation.
If the simulation doesn't change after you change the parameter (and update the diagram) see that same doc page for how to determine why that might be the case.
I'm partially succsefull and I did implement it as you suggested
set_param(mdl,SimulationCommand="start");
set_param(mdl,SimulationCommand="update");
set_param(mdl,SimulationCommand="stop");
btw instead of pause() command I'm trying to monitor SimulationTime and do a decision based on SimulationTime information but it seems is not taking effect ...
How to use the SimulationTime to have update effect at exact time event?
Thank you.
clear
clc
mdl = 'myModel';% The name of the SLX file
open_system('models/myModel.slx');
set_param(mdl,'StopTime','inf');
set_param(mdl,SimulationCommand="start");
while (get_param(mdl,'SimulationTime') < 0.5)
if (get_param(mdl,'SimulationTime') == 0.25)
init_pos = 10;
set_param(mdl,SimulationCommand="update");
end
end
set_param(mdl,SimulationCommand="stop");
That approach is problematic because:
a) there's no guarantee the SimulationTime will ever be exactly 0.25, or even within some tolerance of 0.25 if you were to add a tolerance to that check, unless you've done something in the model itself to ensure SimulationTime exactly hits 0.25
b) even if the SimulationTime does hit 0.25, there's no guarantee it will be at that time when the if statement is checked.
If this logic is what you really want to do (and it's not some simplification just for purposes of this discussion), why not implement that logic directly in the model itself? Should be simple enough to do with a Clock, Compare To Constant, and a Switch.
Is it absolutely necessary to set init_pos at the command line at sim time = 0.25? If so, then there are methods to do so (I believe).
Another approach that might be useful is to use a model operating point. (relevant blog post) Run the simulation for five seconds, save the final operating point, update a parameter, and then run the simulation for another five seconds.
bdclose('all');
hsys = new_system('updatetest');
hsine = add_block('simulink/Sources/Sine Wave','updatetest/Sine');
set_param(hsine,'Amplitude','A');
hout = add_block('simulink/Sinks/To Workspace','updatetest/output');
set_param(hout,'VariableName','y');
PH = 'PortHandles';
add_line(hsys, ...
get_param(hsine,PH).Outport,...
get_param(hout,PH).Inport);
in = Simulink.SimulationInput('updatetest');
in = in.setVariable('A',1);
in = in.setModelParameter('SaveFinalState', 'on');
in = in.setModelParameter('SaveOperatingPoint', 'on');
in = in.setModelParameter('FinalStateName', 'myOp');
in = in.setModelParameter('StopTime','5');
out1 = sim(in);
in = Simulink.SimulationInput('updatetest');
in = in.setModelParameter('LoadInitialState','on');
in = in.setModelParameter('InitialState','out1.myOp');
in = in.setModelParameter('StopTime','10');
in = in.setVariable('A',10);
out2 = sim(in);
figure
plot(out1.y)
hold on
plot(out2.y)

Sign in to comment.

More Answers (1)

It is not possible to change the parametrs of the running Simulink simulation model from Matlab command line while the model is running . The model must be stopped (or perhaps pause()'d) . This is because Simulink runs in a different process.
... Though you could in theory set up udp receive blocks and send data to udp from the command line...

2 Comments

What is the basis for this claim? It's counter to a very specific statement in the documentation (TBF, the documentation uses the term "during simulation" and not "while the simulation is running"), and also counter to actual experience, at least my experience.
It's hard to illustrate here in a scripting mode because scripted commands seem to behave differently than executing commands sequentially at the command line.
Here's the best I can do, hopefully it's convincing.
Define a simple model with a sine wave feeding a To Workspace block
bdclose('all');
hsys = new_system('updatetest');
hsine = add_block('simulink/Sources/Sine Wave','updatetest/Sine');
set_param(hsine,'Amplitude','A');
hout = add_block('simulink/Sinks/To Workspace','updatetest/output');
set_param(hout,'VariableName','y');
PH = 'PortHandles';
add_line(hsys, ...
get_param(hsine,PH).Outport,...
get_param(hout,PH).Inport);
Set infinite stop time and turn on pacing so that simulation run time equals wall clock time
set_param(hsys,'StopTime','inf','EnablePacing','on');
Initial value of amplitude
A = 1;
Start the simulation, pause Matlab for 5 seconds, and then stop the simulation.
set_param(hsys,SimulationCommand="start");
pause(5);
set_param(hsys,SimulationCommand="stop");
I think this figure illustrates that the simulation was running while the Matlab pause was in effect.
figure
plot(out.y)
Start the simulation and pause Matlab for five seconds. Simulation runs while Matlab is paused.
set_param(hsys,SimulationCommand="start");
pause(5);
Change the amplitude of the sine wave and update the diagram.
A = 10;
set_param(hsys,SimulationCommand="update");
Make sure the simulation runs for five more seconds.
pause(5);
set_param(hsys,SimulationCommand="stop");
Plot the output, show the effect of updating the parameter.
figure
plot(out.y)

Sign in to comment.

Categories

Products

Release

R2024b

Asked:

on 8 Jul 2025

Edited:

on 11 Jul 2025

Community Treasure Hunt

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

Start Hunting!