How can I run a Simulink model from appdesigner

59 ビュー (過去 30 日間)
Joan Oliver
Joan Oliver 2021 年 3 月 3 日
回答済み: Captain Karnage 2022 年 10 月 28 日
I wonder where can I get information on how to create an app where I can assign variables for a simulink model. Then run it and show some results on the app layout. Thats the code I have till now, but it won't run the simulation.
% simulink;
%%%%%%%%%%%%RUN_GR_SIMULINK%%%%%%%%%%%%%%%
GR1 = (app.ZGBinEditField.Value/app.ZengineEditField.Value)*(33/20)*(app.ZwheelEditField.Value/app.ZGBoutEditField.Value);
GR2 = (app.ZGBinEditField.Value/app.ZengineEditField.Value)*(30/23)*(app.ZwheelEditField.Value/app.ZGBoutEditField.Value);
GR3 =(app.ZGBinEditField.Value/app.ZengineEditField.Value)*(23/30)*(app.ZwheelEditField.Value/app.ZGBoutEditField.Value);
app.GR1EditField.Value = GR1;
app.GR2EditField.Value = GR2;
app.GR3EditField.Value = GR3;
%%GEOMETRIC CONSTANTS
gravity = 9.81;
bikemass = 150;
rider = 50;
mass = bikemass+rider;
rollcoeff = 0.01;
airdensity = 1.2;
dragcoeff = 0.42;
frontalarea = 0.46;
roadsolpe = app.TrackSlopeEditField.Value;
wheelradius = 0.301;
% orra
%%CENTER OF GRAVITY
h_CoG = app.CoGhmmEditField.Value;
b_CoG = app.CoGbmmEditField.Value;
p_CoG = app.WheelbaseEditField.Value;
%%ALEMBERT MASSES
Alm1 = 12.38 ;
Alm2 = 10.04;
Alm3 = 7.90;
maxenginetorque = 95;
%%%%%%%%%_SETTING_UP_MODEL_VARIABLES_%%%%%%%%%%%
% app.RunSimulinkButton('off', 'Simulating ...');
eprsymin = Simulink.SimulationInput('ePR02_New');
eprsymin = eprsymin.setVariable('gravity',gravity);
eprsymin = eprsymin.setVariable('bikemass',bikemass);
eprsymin = eprsymin.setVariable('mass',mass);
eprsymin = eprsymin.setVariable('rollcoeff',rollcoeff);
eprsymin = eprsymin.setVariable('airdensity',airdensity);
eprsymin = eprsymin.setVariable('dragcoeff',dragcoeff);
eprsymin = eprsymin.setVariable('frontalarea',frontalarea);
eprsymin = eprsymin.setVariable('roadsolpe',roadsolpe);
eprsymin = eprsymin.setVariable('wheelradius',wheelradius);
eprsymin = eprsymin.setVariable('h_CoG',h_CoG);
eprsymin = eprsymin.setVariable('b_CoG',b_CoG);
eprsymin = eprsymin.setVariable('p_CoG',p_CoG);
eprsymin = eprsymin.setVariable('GR1',GR1);
eprsymin = eprsymin.setVariable('GR2',GR2);
eprsymin = eprsymin.setVariable('GR3',GR3);
eprsymin = eprsymin.setVariable('Alm1',Alm1);
eprsymin = eprsymin.setVariable('Alm2',Alm2);
eprsymin = eprsymin.setVariable('Alm3',Alm3);
eprsymin = eprsymin.setVariable('maxenginetorque',maxenginetorque);
% eprsymin.ExternalInput = app.externalInput();
% configure simInp for deployment
eprsymin = simulink.compiler.configureForDeployment(eprsymin);
% DEBUG TIP: Comment out the line above for faster debug
% iterations when runnng this app in the MATLAB desktop.
eprsymin.ExternalInput = app.externalInput();
% run
eprsymout = sim(eprsymin);
% extract and plot the results
eprsimout.speed.time
% errordlg(ME.message);

回答 (2 件)

Chidvi Modala
Chidvi Modala 2021 年 3 月 9 日
The feature that would copy variables from one workspace to another is currently not available in MATLAB. I have brought this issue to the concerned people and it might be considered in any future release.
Currently, there are two workarounds:
1. Use assignin function to store all the variables from AppDesigner/Script to the base workspace.
2. In some cases, the simulation needs many parameters, but only a small number is tuned between runs. In that case, what usually works well is to have a "default" set of data stored either in a Data Dictionary or in the Model Workspace. Then for the data that the App wants to tune, you can create a Simulink.SimulationInput object and use in.setVariable to specify those variables. For the model update, executing in.applyTomodel at the end will apply the changes to the model

Captain Karnage
Captain Karnage 2022 年 10 月 28 日
Use set_param, as follows:
thismodel = 'insert_name_of_your_model_here`;
cs = getActiveConfigSet(thismodel);
cs.set_param('ParameterName', ParameterValue);
I prefer to have one line for each parameter, but you can also have multiple Name/Value pairs in the same statement.
The caveat being is that you will have to change the structure of your code to set the model parameters directly and you'll have to figure out what ParameterName needs to be for each of the variables you want to change, including the names assigned to each of your simulink blocks and their associated parameters using slash '/' notation (alternately, you can set a bunch of string variables that make more sense in english and make their values the names Simulink assigned). It can also be used to set your model settings and parameters like StartTime and StopTime, etc.
There is a guide to set_param on the Mathworks site here: https://www.mathworks.com/help/simulink/slref/set_param.html?s_tid=doc_ta

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by