Error when Trying to use Listner Blocks

3 views (last 30 days)
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)

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 11 Apr 2012
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.
  3 Comments
Paul
Paul on 13 Apr 2012
Im able to solve this problem now. But i am getting a series of error messages.
These are :
??? Error using ==> guimanualflight>Start_Callback at 309
Error due to multiple causes.
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> guimanualflight at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)guimanualflight('Start_Callback',hObject,eventdata,guidata(hObject))
Caused by:
Error using ==> guimanualflight>Start_Callback at 309
Error in 'manualflight/2dplot/sfunxy' while executing MATLAB S-function 'sfunxy', flag = 2
(update), at time 0.0.
Error using ==> guimanualflight>Start_Callback at 309
Invalid handle object.
Error using ==> guimanualflight>Start_Callback at 309
Error evaluating 'StopFcn' callback of XY scope. block (mask) 'manualflight/2dplot'. Invalid
handle object.
Error using ==> guimanualflight>Start_Callback at 309
Invalid handle object.
??? Error while evaluating uicontrol Callback
My code for my listner function of plotting the data im having trouble with . Aswhen the model does work which is rare. It wont plot between the limits of the axis i created andwill create new limits. This is the only code i have that seems to have a slight sign of wrking :
function LocalEventListner(block,eventdata) %#ok
global new_xdata
global new_ydata
xdata = (block.InputPort(1).Data);
ydata = (block.InputPort(2).Data);
new_xdata=[xdata,new_xdata];
new_ydata=[ydata,new_ydata];
p=plot(new_xdata,new_ydata);
set(p,'Parent',axes2)
Kaustubha Govind
Kaustubha Govind on 13 Apr 2012
Try running "dbstop if all error" in MATLAB and then run your file. See if it stops in one of your files and examine the variables that the error is complaining about.

Sign in to comment.

More Answers (1)

Adrian
Adrian on 21 Apr 2020
I add a possible solution.
I had the same problem on a subsystem block containing only an input and a terminator block.
I solved using an Atomic Subsystem. Probably Simulink engine was eliminating my simple block because no real calculation were executed

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!