MATLAB: How to extract data from a specific subplot in a MATLAB figure

data extractionfigureMATLABplotsubplot

I have a matlab figure with 12 subplots (6X2 figures) and I intend to convert certain subplots into data (whose position I know). I have seen tutorials where MATLAB figures where converted to data but could not find one that handled extraction of data from subplots.
I am new to matlab and am having difficulties figuring out how to do this.
Any help is much appreciated!

Best Answer

Hi,
If you don't have the handles of the plot in the figure here is a demonstration for you.
Please read the comments carefully.
%% demo data preallocation
n = zeros(1,12);
x = cell(12,1);
y = cell(12,1);
hFig = figure;
% randomly creating x and y data and plotting
for i = 1:12
n(i) = randi(20,1,1);
x{i} = rand(n(i),1);
y{i} = rand(n(i),1);
hAxes{i} = subplot(6,2,i); % hAxes axis object handles in a cell array (12 handles you have)
hPlot{i} = plot(hAxes{i},x{i},y{i},'LineStyle','none','Marker','*'); % hPlot plot line object handles (12 handles you have)
end
% -------------------------------------------%
% Gathering data from plotted figure
%% FIRST OPTION IN CASE YOU HAVE HANDLES
%% If you know the position use this: (That I choose the 6th position)
myPlotHandle = hPlot{6};
xdata = myPlotHandle.XData;
ydata = myPlotHandle.YData;
% You can also use this from the position: (Axes' children is a plot line object. Our hPlot{6} is hAxes{6}.Children )
xdata = hAxes{6}.Children.XData;
ydata = hAxes{6}.Children.YData;
%%% OR %%%
%% SECOND OPTION IN CASE YOU DON'T HAVE THE OBJECT HANDLES
% click to a point or a data line in the axis. You should click on a data
% plotted.
% run the code below: (gco function gets you the current object(that means last focused or last clicked object), in this case that will be your plot handle like above)
myPlotHandle = gco;
xdata = obj.XData;
ydata = obj.YData;
% you can click for each 12 plot data and run the code so you will get them
% one by one.