How do I extract data from a specific subplot in a MATLAB figure?

176 vues (au cours des 30 derniers jours)
Harishankar Anil Karingathuruthil
Commenté : Luna le 2 Déc 2019
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!

Réponse acceptée

Luna
Luna le 27 Nov 2019
Modifié(e) : Luna le 27 Nov 2019
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.
  2 commentaires
Harishankar Anil Karingathuruthil
Thank you, I tried with my file and it works.
Thank you very much!
Also FYI I used a different code to get the results from an alreaady existing MATLAB figure.
h=openfig('your_file_name.fig');
handles=findobj(h,'Type','line');
x=get(handles(1),'Xdata');
y=get(handles(1),'Ydata');
Here 1 is taken as the index of the subplot.
Cheers!
Luna
Luna le 2 Déc 2019
Your welcome :) Please accept answer if it works, thanks :)

Connectez-vous pour commenter.

Plus de réponses (1)

per isakson
per isakson le 27 Nov 2019
Modifié(e) : per isakson le 27 Nov 2019
This illustrates the old way to do it. (There might be shortcut nowadays.)
%% Example from the documentation of subplot
subplot(2,1,1);
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
%
subplot(2,1,2);
y2 = sin(5*x);
plot(x,y2)
%% Recover x, y1 and y2
ohf = findobj(gcf); % gcf get current figure
%%
oha = findobj( ohf(1), 'Type','axes' ); % find the two axes
%%
oh1 = findobj( oha(1), 'Type','line' ); % find the line of the first axes
%%
all( oh1.XData == x ) % check if the data of the plot is equal to
all( oh1.YData == y2 ) % the data plotted
%%
oh2 = findobj( oha(2), 'Type','line' );
%%
all( oh2.XData == x )
all( oh2.YData == y1 )
Which axes corresponds to which plot? The Position helps to find out
>> oha(1).Position
ans =
0.13 0.11 0.775 0.34116
>> oha(2).Position
ans =
0.13 0.58384 0.775 0.34116
>>
oha(2) is the top one
ADDENDUM
Don't reinvent the wheel before checking with the File Exchange. (as I just did).
  2 commentaires
per isakson
per isakson le 27 Nov 2019
Modifié(e) : per isakson le 27 Nov 2019
ohf = findobj(gcf); % gcf get current figure
%%
oha = findobj( ohf(1), 'Type','axes' );
may be replaced by a better way
oha = findobj( gcf, 'Type','axes' );
The figures can alternatively be found by
>> ohf = findobj( 0, 'Type', 'figure' );
>> ohf
ohf =
Figure (1) with properties:
Number: 1
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [680 678 560 420]
Units: 'pixels'
With many figures one has to find a way to distinguish between them.
It's more robust not to use gcf, gca, gco in code.
Harishankar Anil Karingathuruthil
Thank you for the codes,
FYI I also found another method too, please check the comment in the above post!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Objects dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by