Simulink parameters change during simulation from Matlab cmd line

5 views (last 30 days)
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

Paul
Paul on 8 Jul 2025
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
Paul
Paul on 9 Jul 2025
Edited: Paul on 10 Jul 2025
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).
Paul
Paul on 11 Jul 2025
Edited: Paul on 11 Jul 2025
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)

Walter Roberson
Walter Roberson on 9 Jul 2025
Edited: Walter Roberson on 9 Jul 2025
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
Paul
Paul on 9 Jul 2025
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.
Paul
Paul on 9 Jul 2025
Edited: Paul on 9 Jul 2025
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

Find more on Simulink Environment Customization in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!