Access app designer variables in Simulink

8 views (last 30 days)
RW Student
RW Student on 16 Jan 2019
Commented: Amanda Quan on 2 Apr 2021
Hi there,
I am trying to make an interface around a simulink simulation using app designer. An user should be able to input values for certain parameters. When he/she clicks "Simulate", the application should simulate the Simulink model with the given parameters. The application and simulink file are in the same folder. I used the code below to set parameters and simulate the model. However, I get the error 'Undefined function or variable [...] Variable does not exist'. In other words, the Simulink model cannot access the variables created in the app designer. Any suggestions? FYI: running the Simulink file from a Matlab script works fine.
Variable1 = app.Variable1.Value;
Variable2 = app.Variable2.Value;
simOut = sim('simulinkFile','ReturnWorkspaceOutputs','on');
  2 Comments
Benjamin Quickfall
Benjamin Quickfall on 14 Oct 2019
Hi,
I think your problem is that you are only creating (and setting the values for) Variable1 and Variable2 within the app. I.e. your Simulink model cannot see those variables as they are only accessible within the app.
What you need to do is make those variables accessible to the Simulink Model. This depends on where your Simulink Model is sourcing it's variables. If you open your Simulink model and click on the "Model Explorer" icon (located under the modelling tab of the ribbon bar if using the 2019b verion, google it if you're unsure what it looks like or find its location in a different version of Matlab Simulink). The Model Explorer will show where your Simulink model sources the variables it uses. The potential sources are: Base Workspace, Model Workspace, Data Dictionary.
If the variables you want to change within your App are sourced from the Base Workspace then you need to create/update variables from your App to the Base Workspace. Likewise for the Model Workspace.
I have only used an App to update Data Dictionary sourced variables so I don't want to try to advise for Base/Model Workspace sourced variables but hopefully I've given you an idea of what to google if this is your situation.
If your Simulink Model is sourcing from a Data Dictionary, this is what I have done to update my Data Dictionary variables within the App's code:
Under the callback function for your run simulation button:
% open you data dictionary file
DictionaryObj = Simulink.data.dictionary.open('location of your .sldd file')
% Access the Design Data of the Data Dictionary (Data Dictionaries have 3 sections)
dDataSectObj = getSection(DictionaryObj, 'Design Data')
% Get the object of the variable you want your app to update
variableObj = getEntry(dDataSectObj, 'variable of interest name used in Simulink Model')
% Set the variable's value
setValue(variableObj, app.component.Value)
% Run your simulation
sim('ModelName')
Amanda Quan
Amanda Quan on 2 Apr 2021
"Under the callback function for your run simulation button"
Can you help me out on where this is? I have a function that executes when my Start/Stop button is pushed, but that's not where I'm interfacing callbacks (I don't think) - the callbacks are listed out within my function which creates the SimulationInput object. Which of those two areas (or another one entirely) is "under the callback function for my run simulation button"?
(Caveat I'm an extreme newbie and got most of my code from the Mathworks LorenzSystemApp example, but they use set_param to set the variables within their Simulink model and for whatever reason that isn't working for me so I'm looking for alternatives.)

Sign in to comment.

Answers (0)

Categories

Find more on Interactive Model Editing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!