MATLAB: How to keep parameter and variable workspace and simulink visibility when running a *.m file from a GUI

appdesignergui workspacepassing parameters to gui

I currently have a system that is partitioned into three files.
mySystem.m
setSystemParameters.m
mySystemModel.slx
I run mySystem.m from the command line
>> mySystem
which runs setSystemParameters.m and then launches the simulink model.
135 ...
136 tend = 0.011;
137 setSystemParameters;
138 ...
139 set_param('mySystemModel','StartTime', '0.0096', 'StopTime',string(tend))
140 sim('mySystemModel');
141 % start post processing
142 ...
this all runs fine and the workspace shows all the variable and parametric values and they are passed to the simulink model without issue.
I then created a GUI so the user can select system frequency from a set of radio buttons and a RUN PushButton to run mySystem.m after the desired frequency is selected. The frequency is being passed to mySystem.m properly and the mySystem.m runs when 'RUN' is pressed.
The problem is that the variables no longer apear in the workspace and the simulink nolonger has visibility to the values set in the mySystem.m and setSystemParameters.m files. I get a series of errors similar to:
Error using mySystem (line 140)
Invalid setting in 'mySystemModel/Constant' for parameter 'Value'.
Error using mySystem (line 140)
Error evaluating parameter 'Value' in 'mySystemModel/Constant'
Error using mySystem (line 140)
Undefined function or variable 'DCin'.
'Constant' is a simulink library element with parameter 'Vaule' which is set in setSyetemParameters.m to DC in.
How do I maintain workspace and simulink visibility to the parameters after I launch the mySystem From the GUI?

Best Answer

Your variables will now be living in the function workspace of your GUI function. By default, Simulink is looking in the MATLAB base workspace for input data. If you don't want to change your model then one of the easiest solutions (not the only solution) is to assign your variables back in to the MATLAB base workspace by using the assignin command within your GUI code.
If changing your Simulink model is up for grabs then take a look at Data Dictionaries which are much cleaner and get rid of variables in the base workspace. https://uk.mathworks.com/help/simulink/ug/what-is-a-data-dictionary.html