How do I extract data from a specific subplot in a MATLAB figure?
Show older comments
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!
Accepted Answer
More Answers (1)
per isakson
on 27 Nov 2019
Edited: per isakson
on 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
2 Comments
per isakson
on 27 Nov 2019
Edited: per isakson
on 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
on 30 Nov 2019
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!