How can I update a GUI with values from my Simulink model as it is running?

13 次查看(过去 30 天)
I have a Simulink model and I would like to display the value of the output of certain blocks in my GUI.

采纳的回答

Deepti Sukhwani
Deepti Sukhwani 2011-1-11
There is no direct way to access the runtime parameters of a Simulink model from a MATLAB GUI. 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.
1. Create a GUI with a control that you would like to update with information from the model. In this example, 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.
%The GUI handles are by default hidden, turn them on
set(0,'ShowHiddenHandles','on');
%Set up the arguments that will go into the gain block event callback listener
blk = 'mytestmdl/Gain';
event = 'PostOutputs';
listener = @updategui;
%Create the listener
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)
%create a run time object that can return the value of the gain block's
%output and then put the value in a string.
rto = get_param('mytestmdl/Gain','RuntimeObject');
str = num2str(rto.OutputPort(1).Data);
%get a handle to the GUI's 'current state' window
statestxt = findobj('Tag','curState');
%update the gui
set(statestxt,'string',str);
  3 个评论
Kaustubha Govind
Kaustubha Govind 2013-8-9
Rinachi: As the answer suggests, you need to put the code in the model's 'StartFcn' callback. See Model Callbacks for information about the StartFcn callback.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Event Functions 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by