Finding and Saving GUI axes' xdata ydata as Userdata

5 views (last 30 days)
I have a GUI (using guide) which processes some data files and generates a plot in one axes in the GUI. The plot is made by the spectopo function, which is a part of EEGlab toolbox and displays power spectral density from EEG data. Once the plot is made, I set some of the properties of the plot, like xlim, ylim, font sizes etc. I want to store the current plot's Xdata and Ydata (preferably after axis limits have been set), in the Userdata for this axes. So that I can use this data and read out the y-value of this data for a particular x-value - when another button in GUI is pressed.
any suggestions to do that ? Once the plot is made, there is no Xdata or Ydata property for this axis. where is this data stored ? or should i store that data from the main function spectopo which is actually generating the plot ? Thanks

Accepted Answer

Geoff Hayes
Geoff Hayes on 30 Sep 2014
Raul - the spectopo function seems to make use of calls to plot (or at least, the version at http://sccn.ucsd.edu/eeglab/allfunctions/spectopo.html seemed to) in order to display data on the axes. If that is the case, then you may be able to do something like the following in order to get the x and y data that you wish.
Suppose we are plotting the sine and cosine curves on an axes object
t=-2*pi:0.0001:2*pi;
u=sin(t);
v=cos(t);
hold(handles.axes1,'on');
plot(handles.axes1,t,u,'b');
plot(handles.axes1,t,v,'g');
In order to get the x and y data from each call to plot, we need to access the 'children' of the axes
% get handles to the children of the axes
hChildren = get(handles.axes1,'Children');
In this example, because we plotted two curves on the axes, hChildren is a 2x1 vector of handles to the plot graphics objects.
To get the x and y data from the first handle (which may not correspond to the first plot), we do
xData1 = get(hChildren(1),'XData');
yData1 = get(hChildren(1),'YData');
Note that this will be all of the data that has been plotted, even after you have set the limits on the axes.
If you're okay with storing all of this data, then just save it to the handles structure as
handles.xdata1 = xData1;
handles.ydata1 = yData1;
guidata(hObject,handles); % <--- important that we call this to save the data
Now you can access the x and y data as fields from the handles structure within any other callback.
  1 Comment
Raul
Raul on 1 Oct 2014
Edited: Raul on 1 Oct 2014
Thanks Geoff Hayes, your clear answer gave me the hint about what I was doing wrong. When finding children for the axes object, I had thought that one of the children would be x/ydata itself. Now I understood that each plot will be a child and then I can extract the data from that plot. This was further complicated by the fact that when getting children handles from the axes, there were some extra handles in addition to the EEG channel data. And saving that data, the way u showed, seems better than storing it in UserData for the axes.
I need some advice on another issue. Say I process one file and it generates the power spectral density plot on the GUI axes. Then I need to process another file, which should replace the current plot with another plot. however, with spectopo, when the next plot is put in the axes, the x-ylabels and ticks are somehow removed. I had faced this problem earlier when I was plotting and saving the generated PSD figures as PNG image files. On the second run of spectopo, if a figure was already open, this problem would happen. So I closed the earlier figure, and opened a new figure, so that the second run of spectopo can plot on that - and then everything was normal. Here I tried to use delete (axeshandle) - in the next call to plot using spectopo, no object is found, or plot is done on the whole GUI figure. cla - clears the plot, but still causes the same problem cla (reset) only seems to reset the properties of the axes, does not delete the old plot .sometimes gives error - too many output arguments. clf would delete the whole GUI figure.
Any suggestions ?
ok, wait....
reset(gca); % seems to be working

Sign in to comment.

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output 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!