MATLAB: Finding and Saving GUI axes’ xdata ydata as Userdata

axesguiguideplotxdataydata

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

Best Answer

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.