Error when Trying to use Listner Blocks
Show older comments
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
More Answers (1)
Adrian
on 21 Apr 2020
1 vote
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 Event 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!