필터 지우기
필터 지우기

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

조회 수: 10 (최근 30일)
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

댓글을 달려면 로그인하십시오.

추가 답변 (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 CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by