MATLAB: Error when Trying to use Listner Blocks

add_exec_event_listnerguimatlab guisimsimulink

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

Try changing:
set_param('manualflight','StartFcn',localAddEventListener);
To:
set_param('manualflight','StartFcn','localAddEventListener');
StartFcn needs to be set to a string containing the MATLAB code to be executed. In the first case, the SET_PARAM function is trying to run the function localAddEventListener, with the expectation that it will return a string that must then be set as StartFcn. So the add_exec_event_listener call is made at the time that the set_param command is run, instead of when the model starts executing.