Reading data from Simulink to GUI using event listener

33 views (last 30 days)
Hi,
I have successfully updated the GUI from a simulink with event listener for a simple example. I am basically follow this web How can I update a GUI with values from my Simulink model as it is running?. However, it does not update on GUI when I connected it with my arduino (s-function is used). After that, I checked it on the command window with the following code and the GUI displayed the reading on the Gain block without streaming the data.
>> rto1 = get_param('Sfunction3/Gain','RuntimeObject');
>> str1 = num2str(rto1.OutputPort(1).Data);
>> statestxt1 = findobj('Tag','edit2');
>> set(statestxt1,'string',str1);
I think this is an issue from event listener but I couldn't find any wrong. Please help me if you have any clue.
%StartFcn
%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 = 'Sfunc/Multiply';
blk1 = 'Sfunc/Gain1';
blk2 = 'Sfunc/Gain2';
blk3 = 'Sfunc/Gain3';
event = 'PostOutputs';
listener = @updategui;
%Create the listener
h = add_exec_event_listener(blk, event, listener);
h1 = add_exec_event_listener(blk1, event, listener);
h2 = add_exec_event_listener(blk2, event, listener);
h3 = add_exec_event_listener(blk3, event, listener);
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('Sfunc/Multiply','RuntimeObject');
rto1 = get_param('Sfunc/Gain1','RuntimeObject');
rto2 = get_param('Sfunc/Gain2','RuntimeObject');
rto3 = get_param('Sfunc/Gain3','RuntimeObject');
str = num2str(rto.OutputPort(1).Data);
str1 = num2str(rto1.OutputPort(1).Data);
str2 = num2str(rto2.OutputPort(1).Data);
str3 = num2str(rto3.OutputPort(1).Data);
%get a handle to the GUI's 'current state' window
statestxt = findobj('Tag','edit1');
statestxt1 = findobj('Tag','edit2');
statestxt2 = findobj('Tag','edit3');
statestxt3 = findobj('Tag','edit4');
%update the gui
set(statestxt,'string',str);
set(statestxt1,'string',str1);
set(statestxt2,'string',str2);
set(statestxt3,'string',str3);

Accepted Answer

Shubham
Shubham on 4 May 2023
Hi gdz,
It is possible that the issue lies in the updategui() function not being called when the Simulink model is connected to the Arduino using an S-function. This could be because the PostOutputs event is not being triggered in this case.
One way to check if this is the issue is to add some debugging statements to the updategui() function to see if it is being called at all. For example, you could add a line like disp('updategui called') at the beginning of the function to see if it is being executed.
If the function is not being called, you may need to use a different event to trigger the function when the Simulink model is connected to the Arduino. You could try using the PostProposedDT event instead, which is triggered after the sample time of the model has elapsed. To do this, you would need to change the event argument in the add_exec_event_listener() function to 'PostProposedDT'.
If the function is being called but the GUI is still not updating, you could try adding some debugging statements to see if the values of rto1.OutputPort(1).Data and str1 are being updated correctly. For example, you could add a line like disp(str1) after the line str1 = num2str(rto1.OutputPort(1).Data) to see if the value is changing.
If none of these suggestions solve the issue, it may be helpful to provide more information about the Simulink model and the S-function being used, as well as any error messages that are being displayed.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!