Update Simulink model in runtime via simulinkApp and adding "SimulationTime"?

3 次查看(过去 30 天)
I'm building App for my simulink model with use of appdesigner. I need to add a StopTime for this model through App - how this top time is accesible and to write in via App?
I need to initialize and write in runtime (during Simulink simulation run) to variables of that model. This varialble are initialiy define in "init.m" script wich is run prior simulink run and I use as constant inputs in Simulink model - how to update them correfcly during simulation run via App?
Certain variables are need to display onlin (in runtime) bit I do not need time -plot for it - is it some display box for this?

回答(1 个)

Shishir Reddy
Shishir Reddy 2025-7-18
Hi Jack
As per my understanding, you would like to build a Simulink-based application using App Designer and is looking for guidance on three main topics - Setting stop time, Modifying variables, Displaying variables values live.
1. Set the Simulink Stop Time from App Designer
You can set the stop time of the Simulink model programmatically using -
set_param('YourModelName', 'StopTime', '10') % Example: stop time = 10 seconds
If you want to allow user input for the stop time -
stopTime = app.StopTimeEditField.Value; % Assuming StopTimeEditField is a numeric input in your App
set_param('YourModelName', 'StopTime', num2str(stopTime));
2. Update Runtime Variables Initialized in 'init.m'
If your variables are initialized in an 'init.m' script and used as block parameters, they typically only take effect before simulation starts.
To modify them during simulation, consider using Simulink.Signal objects and 'set_param' or 'setVariable' to change values in the base workspace.
set_param('YourModelName/YourConstantBlock', 'Value', 'newValue');
3. Display Variable Values in Runtime (without plot)
You can use a Numeric Edit Field or Label in App Designer to display the values of variables during simulation. You can periodically update it using a timer or a loop.
app.myTimer = timer('ExecutionMode', 'fixedRate', 'Period', 1,'TimerFcn', @(~,~) updateDisplay(app));
start(app.myTimer);
function updateDisplay(app)
val = evalin('base', 'yourVariable'); % or use get_param if it's from a block
app.ValueDisplayField.Value = val; % Update UI component
end
Remember to stop and delete the timer when you stop the simulation.
I hope this helps.

类别

Help CenterFile Exchange 中查找有关 Verification, Validation, and Test 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by