There is no direct way to access the runtime parameters of a Simulink model from a MATLAB GUI without the use of some callback function. To work around this issue, you need to create a function that will update the GUI with the desired model data at each time step. An outline of this procedure is as follows.
NOTE: This solution is specific to normal mode simulation. It does not work when the model is run in accelerator mode or built into an executable. In these cases, one way to get the model to work is to write the value to a text file in the model, and then setup a timer to read the value from the text file in the GUI.
You can download the attached model and GUI to see a working example.
1. Create a GUI with a control that you would like to update with information from the model. In this case we would like to see the output of the model's Gain block in the GUIs edit text window.
2. Create a model callback function that will register an event listener for the block in question. This function will be called every time the block is updated. We will use it to access the value of the Gain blocks output and pass them to the GUI.
In this case we will use the models 'StartFcn' callback.
set(0,'ShowHiddenHandles','on');
blk = 'mytestmdl/Gain';
event = 'PostOutputs';
listener = @updategui;
h = add_exec_event_listener(blk, event, listener);
3. Create a MATLAB file function that will get the Gain block's runtime output parameter and pass it to the GUI.
function varargout = updategui(varargin)
rto = get_param('mytestmdl/Gain','RuntimeObject');
str = num2str(rto.OutputPort(1).Data);
statestxt = findobj('Tag','curState');
set(statestxt,'string',str);
You can try out an example by following the steps below.
1. Download the attached files
mytestgui.m
mytestmdl.mdl
mytestgui.fig
updategui.m
2. Open the model file.
3. Open the GUI by running the file 'mytestgui.m'
4. Start the simulation by clicking the 'Start' button in the GUI.
5. You can change the value of 'Gain' in the GUI while the simulation is running.
6. Click 'Stop' in the GUI to halt the simulation.
Best Answer