I am trying to Add a listener to a Scope block in simulink from a GUI. I seem to be adding the listener at the wrong time I think.
The error I am getting is
??? Error using ==> add_exec_event_listener at 94 Can add listener only when block diagram is executing.
Error in ==> guimanualflight>localAddEventListener at 367 eventhandle=add_exec_event_listener('manualflight/xyplot','PostOutputs',@LocalEventListner);
Error in ==> guimanualflight>Start_Callback at 309 set_param('manualflight','StartFcn',localAddEventListener);
This i thought meant that i should add the listener block after starting the simulation. But this also gave me the same result. I am using this guide to listners as a basis :
The only real difference in the code i am using is that i use the sim command instead of the set_param function to start the model. Does this make a difference?
my code is as follows:
% — Executes on button press in Start. function Start_Callback(hObject, eventdata, handles) %#ok % hObject handle to Start (see GCBO) % eventdata reserved – to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % set the stop time to inf
open_system('manualflight.mdl') %opens The Model so control can be taken of it if GUI crashes
set_param('manualflight','BlockReduction','off') figure(guimanualflight) %returns focus to the GUI
set_param('manualflight/flight_plan/xset', 'Gain', '0') %set initial points as 0.0.0.0
set_param('manualflight/flight_plan/yawset', 'Gain', '0') set_param('manualflight/flight_plan/zset', 'Gain', '0') set_param('manualflight/flight_plan/yset', 'Gain', '0'); set_param('manualflight','StartFcn',localAddEventListener); options=simset('SrcWorkspace','current','DstWorkspace','base') ;sim('manualflight',[0 Inf],options);
global new_xdata global new_ydata new_xdata=[]; new_ydata=[];
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Callback Function for executing the event listener on the scope block % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function eventhandle=localAddEventListener
eventhandle=add_exec_event_listener('manualflight/xyplot','PostOutputs',@LocalEventListner);
function LocalEventListner(block,eventdata)
global new_xdata global new_ydata
xdata = get(block.OutputPort(1).Data); ydata = get(block.OutputPort(2)); new_xdata=[xdata,new_xdata]; new_ydata=[ydata,new_ydata];
axes(handles.axes1) cla axis ([-1 1 -1 1]) grid on plot(new_xdata,new_ydata)
Best Answer