Multiple Event Listeners are plotting last active axes on GUI
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a Simulink model and I want to access some of its data while the simulation is running and show them on GUI. I used event listener, Simulink Signal Viewing using Event Listeners and a MATLAB UI. and https://matlabideas.wordpress.com/2011/11/02/guideing-simulink-with-matlab/ as basis for my work. I added 2 listeners to different gain bloks and wanted to show them as separate axes on GUI. I have a simple GUI, start button, stop button and 3 axes (axes1, axes2, and axes3) and designed it using guide.
my code :
function startbutton_Callback(hObject, eventdata, handles)
% hObject handle to startbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% When the model starts, call the localAddEventListener function
global a;
a = get_param('GUImodel','StartFcn');
set_param(handles.ModelName,'BlockReduction','off');
set_param(handles.ModelName,'StartFcn','localAddEventListener');
% Start the model
set_param(handles.ModelName, 'SimulationCommand', 'start');
function eventhandle = localAddEventListener
eventhandle(1) = add_exec_event_listener('GUImodel/Gain', ...
'PostOutputs', @localEventListener);
eventhandle (2) = add_exec_event_listener('GUImodel/Gain1', ...
'PostOutputs', @localEventListener2);
function localEventListener(block,eventdata)
hf = findall(0,'Tag',mfilename);
handles = guidata(gcf);
global ph;
% Gets the time and output value
simTime = block.CurrentTime;
simData = block.OutputPort(1).Data;
% Gets handles to the point coordinates
xData = get(ph(1),'XData');
yData = get(ph(1),'YData');
% Displaying only the latest n-points
n = 200;
if length(xData) <= n
xData = [xData simTime];
yData = [yData simData];
else
xData = [xData(2:end) simTime];
yData = [yData(2:end) simData];
end
% Update point coordinates
set(ph(1),...
'XData',xData,...
'YData',yData);
plot(ad.handles.axes1,ph(1).XData,ph(1).YData);
% The axes limits need to change as you scroll
samplingtime = .01;
offset = samplingtime*n;
newXLim = [max(0,simTime-offset) max(offset,simTime)];
set(handles.axes1,'XLim',newXLim);
function localEventListener2(block, eventdata)
hf = findall(0,'Tag',mfilename);
handles = guidata(gcf);
global ph ;
% Gets the time and output value
simTime = block.CurrentTime;
simData = block.OutputPort(1).Data;
% Gets handles to the point coordinates
xData = get(ph(2),'XData');
yData = get(ph(2),'YData');
% Displaying only the latest n-points
n = 200;
if length(xData) <= n
xData = [xData simTime];
yData = [yData simData];
else
xData = [xData(2:end) simTime];
yData = [yData(2:end) simData];
end
% Update point coordinates
set(ph(2),...
'XData',xData,...
'YData',yData);
plot(handles.axes2,ph(2).XData,ph(2).YData);
% The axes limits need to change as you scroll
samplingtime = .01;
offset = samplingtime*n;
newXLim1 = [max(0,simTime-offset) max(offset,simTime)];
set(handles.axes2,'XLim',newXLim1);
I could get values from listeners but the problem is that on the GUI screen these values were plotted on only the last active axes (for my GUI it's axes3) as 2 different signals and other axes showed nothing. As you can see from the code, I wanna plot them on axes 1 and axes2. when I removed the axes3, data from listeners were plotted on axes2. I couldn't separate the listeners and couldn't show the data on the axes I want.
the codes of
plot(handles.axes1,ph(1).XData,ph(1).YData);
plot(handles.axes2,ph(2).XData,ph(2).YData);
are useless. Also
hf = findall(0,'Tag',mfilename);
handles = guidata(gcf);
are useless because when I removed them, nothing changes. I read Event listener not picking up GUI data and axes(handles.axes1) doesn't work in localEventListener function in GUI , I think it is a GUI handles problem, but I couldn't solve it.
Thank you in advance. Regards
3 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Event Functions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!