フィルターのクリア

axes(handles.axes1) doesn't work in localEventListener function in GUI

10 ビュー (過去 30 日間)
Milad Abdollahi
Milad Abdollahi 2015 年 7 月 30 日
回答済み: Milad Abdollahi 2015 年 8 月 1 日
Hi everybody I made a simulink system (Vessel) and use GUI for visualization of this. The GUI contains an axes and a pushbutton. When pushbutton is pressed program load 'Vessel' and start simulation. I added "add_exec_event_listener" to get output parameter of Gain block and plot it in GUI axes. But the problem is when I run mfile this error appears referring to axes(handles.axes1) in localEventListener function: Undefined variable "handles" or class "handles.axes1". Could you help me please? Here is code for pushbutton:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)figure(2)
global new_time new_temp
new_time=[];
new_temp=[];
ModelName = 'Vessel';
% Opens the Simulink model
open_system(ModelName);
% Simulink may optimise your model by integrating all your blocks. To
% prevent this, you need to disable the Block Reduction in the Optimisation
% settings.
set_param(ModelName,'BlockReduction','off');
% When the model starts, call the localAddEventListener function
set_param(ModelName,'StartFcn','localAddEventListener');
% Start the model
set_param(ModelName, 'SimulationCommand', 'start');
% When simulation starts, Simulink will call this function in order to
% register the event listener to the block 'SineWave'. The function
% localEventListener will execute everytime after the block 'SineWave' has
% returned its output.
function eventhandle = localAddEventListener
eventhandle = add_exec_event_listener('Vessel/Temp/Gain1', ...
'PostOutputs', @localEventListener);
% The function to be called when event is registered.
function localEventListener(block, eventdata)
% Gets the time and output value
global new_time new_temp
time = block.CurrentTime;
temp = block.OutputPort(1).Data;
new_time=[new_time,time];
new_temp=[new_temp,temp];
axes(handles.axes1)
plot(new_time,new_temp)

採用された回答

Sean de Wolski
Sean de Wolski 2015 年 7 月 30 日
handles contains the handles to the user interface and need to be passed in explicitly to the callback. When you register the listener, rather than using strings, use anonymous functions which can capture the workspace variables, in this case handles.
% When the model starts, call the localAddEventListener function
set_param(ModelName,'StartFcn',@()localAddEventListener(handles));
And
function eventhandle = localAddEventListener(handles)
eventhandle = add_exec_event_listener('Vessel/Temp/Gain1', ...
'PostOutputs', @(src,evt)localEventListener(src,evt,handles));
Then add handles to the localEventListener signature too:
% The function to be called when event is registered.
function localEventListener(block, eventdata,handles)
  4 件のコメント
Milad Abdollahi
Milad Abdollahi 2015 年 7 月 30 日
編集済み: Milad Abdollahi 2015 年 7 月 30 日
hope this time zip file is loaded correctly. this code didn't work. I guess it has some mistakes in functions which called in pushbutton callback
Sean de Wolski
Sean de Wolski 2015 年 7 月 30 日
still not a valid zip file :(

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

その他の回答 (1 件)

Milad Abdollahi
Milad Abdollahi 2015 年 8 月 1 日
The problem is solved by adding "handles=guidata(gcbo)" to locadAddEventListner function and use Sean de Wolski suggestion. Here is corrected codes:
function eventhandle = localAddEventListener
%show hidden handles of GUI in this function
set(0,'ShowHiddenHandles','on');
handles=guidata(gcbo);
eventhandle = add_exec_event_listener('Vessel/Temp/templisten', ...
'PostOutputs',@(block,eventdata)localEventListener(block,eventdata,handles));
% The function to be called when event is registered.
function localEventListener(block, eventdata,handles)
set(0,'ShowHiddenHandles','on');
axes(handles.axes1);
% Gets the time and output value
global new_time new_temp
time = block.CurrentTime;
temp = block.OutputPort(1).Data;
%updating variable for plot
new_time=[new_time,time];
new_temp=[new_temp,temp];
plot(new_time,new_temp)
grid on

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by