How do I extract data from a specific subplot in a MATLAB figure?
이전 댓글 표시
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!
채택된 답변
추가 답변 (1개)
per isakson
2019년 11월 27일
편집: per isakson
2019년 11월 27일
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
per isakson
2019년 11월 27일
편집: per isakson
2019년 11월 27일
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
2019년 11월 30일
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!