Event listener not picking up GUI data
Mostrar comentarios más antiguos
I've been trying to learn how to use an event listener (been using this as a basis: Simulink Signal Viewing using Event Listeners and a MATLAB UI.) and i seem to of found a strange error:
hf =
0x0 empty GraphicsPlaceholder array.
gui_name =
Event_listener_GUI
Error using guidata (line 87)
H must be the handle to a figure or figure descendent.
Error in Event_listener_GUI>localEventListener (line 232)
handles = guidata(mfilename)
Warning: Error occurred while evaluating listener callback.
The code that generates this is below (I removed the ';' on a few lines because I wanted to see what they were doing as well as un/commented out code from the simpleGUI.m file by Phil Goddard as i wanted to compare what his function was doing compared to mine, as it turns out his retrieves data from his GUI but mine displays what's above):
function localEventListener (block, eventdata)
% Get the application data
hf = findall(0,'Tag',mfilename)
gui_name = mfilename
%hf = gcbo
handles = guidata(mfilename)
% Get the handle to the line that needs updating
thisLineHandle = handles.ad.LineHandles([handles.ad.viewing.BlockHandle]...
==block.BlockHandle);
% Get current data for the line
xdata = get(thisLineHandles, 'XData');
ydata = get(thisLineHandles,'YData');
% Get the simulation time
sTime = block.CurrentTime;
data = block.InputPort(1).Data;
% only the last 1001 points worth of data is needed, the model sample time
% is 0,001 so this represents 1000 seconds of data
if length(xdata)<1001
newXData = [xdata sTime];
newYData = [ydata data];
else
newXData = [xdata(2:end) sTime];
newYData = [ydata(2:end) data];
end
% Display the new dataset
set(thisLineHandle,'XData',newXData,'YData',newYData);
% The axes limits might also need altering
newXLim = [max(0,sTime-10) max(10,sTime)];
set(handles.axes1,'XLim',newXLim);
For some reason when the model is started using the GUI (GUI remains open throughout) and the event listener is called it fails. any hope would be much appreciated!
6 comentarios
Adam
el 22 de Oct. de 2018
For future reference:
gcf is always somewhat dodgy to use in any code other than temporary scripts. It relies entirely on the 'current figure' being what you expect it to be. At a guess the findall line makes the figure you expect the current figure, although I couldn't say for sure.
You should use explicit handles for figures, axes, plots and anything else you want to refer to again to avoid unexpected bugs popping up.
Adam
el 22 de Oct. de 2018
Using mfilename definitely won't work. I don't use Simulink so I don't know how things work there, but when I create a figure I keep its handle if I know I will need it again e.g.
hFig = SomeGUI( );
Then I can just use hFig as the figure handle later on instead of gcf.
Matthew
el 22 de Oct. de 2018
Adam
el 23 de Oct. de 2018
You can change the 'Tag' in guide and I always do. By default it is just 'figure1' which is not very helpful. You still have to search for it afterwards if you rely on the tag, but if you are not able to keep the GUI handle itself for some reason it is the next best thing.
Respuesta aceptada
Más respuestas (1)
Matthew
el 22 de Oct. de 2018
Categorías
Más información sobre Event Functions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!