MATLAB Answers

Reading a wire value from Simulink into the Command Window or GUIDE

63 ビュー (過去 30 日間)
River Rock
River Rock 2012 年 8 月 20 日
編集済み: John Kelly 2016 年 7 月 12 日
I want to read the values that passes through a wire in my Simulink model.
This value should be displayed in a text field on a MATLAB GUI (it's the water level in a tank that is acquired every 100 ms).
I found out about get_param instruction, but until now I only managed to get the values from constants and transfer functions; example: get_param('main/Constant','Value') ; get_param('main/Transfer Function','Denominator').
How do I address a wire and its property list (value)?
The data is generated by an OPC Read block, passing through a subsystem using an Out port.

  0 件のコメント

サインインしてコメントする。

採用された回答

Kaustubha Govind
Kaustubha Govind 2012 年 8 月 20 日
編集済み: John Kelly 2016 年 7 月 12 日

  0 件のコメント

サインインしてコメントする。

その他の回答 (1 件)

River Rock
River Rock 2012 年 8 月 21 日
Thanks for your answer; I was able to get the data from the filter's output
data=get_param('main/level/level_filter','RuntimeObject'); lvl = data.OutputPort(1).Data;
It works ok by request
How can I display this data cyclically in a Static Text field on a GUI ? Is there some callback that runs periodically ( and the sample time can be specified) ?

  4 件のコメント

表示 1 件の古いコメント
River Rock
River Rock 2012 年 8 月 22 日
I added the following code in the gui m file:
function pushbutton1_Callback(hObject, eventdata, handles)
set_param('main','SimulationCommand','start');
iev = add_exec_event_listener('main/nivel/MATLAB Function','PostOutputs',@fetch_data);
function fetch_data(block, eventData)
set(handles.text3,'String','does_it_work');
Doesn't seem to work. What am I doing wrong?
UPDATE: It works if I type the add_exec_event_listener instruction in the Command Window, provided that fetch_data is a stand-alone function. In the GUI it still doesn't work.
Kaustubha Govind
Kaustubha Govind 2012 年 8 月 22 日
Is fetch_data part of the GUI function? I think that it should indeed be a standalone function. You can cache the GUI "handles" object onto the block's UserData. Haven't tested this myself, but how about:
In GUI file:
function pushbutton1_Callback(hObject, eventdata, handles)
set_param('main/nivel/MATLAB Function', 'UserData', handles);
set_param('main','SimulationCommand','start');
iev = add_exec_event_listener('main/nivel/MATLAB Function','PostOutputs',@fetch_data);
In a standalone function:
function fetch_data(block, eventData)
handles = get_param(block.BlockHandle,'UserData');
set(handles.text3,'String','does_it_work');
River Rock
River Rock 2012 年 8 月 23 日
I found a workaround yesterday. But your solution looks more elegant, I had to use global variables.
Thanks for your great support, the problem is solved.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by