필터 지우기
필터 지우기

Extracting data from Matlab .fig with 2 different y-axes?

조회 수: 28 (최근 30일)
Bakr Abdelgaliel
Bakr Abdelgaliel 2022년 6월 22일
댓글: Star Strider 2022년 6월 23일
I have a figuer with two different y-axes, and i would like to extract both of the data with its relevant x-axis data. I have used the following script, but their is an error when writing axesObj(2)!
fig=openfig('MyFig.fig');
axesObj = get(gcf, 'Children');
datObj1 = get(axesObj(1), 'Children');
datObj2 = get(axesObj(2), 'Children');
xdat1 = get(datObj1, 'XData');
ydat1 = get(datObj1, 'YData');
xdat2 = get(datObj2, 'XData');
ydat2 = get(datObj2, 'YData');
  댓글 수: 3
Jan
Jan 2022년 6월 22일
@Bakr Abdelgaliel: Whenever you mention an error in the forum, attach a copy of the complete meesage. It is easier to fix an error than to guess, what the error is.
Bakr Abdelgaliel
Bakr Abdelgaliel 2022년 6월 22일
@Image Analyst thanks for your reply.
I have attached the file now. unfortunatly, I dont have the original data.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2022년 6월 22일
Try something like this —
x = linspace(0, 10);
y1 = sin(2*pi*x/max(x));
y2 = exp(-0.2*x) .* cos(3*pi*x/max(x));
figure
yyaxis left
plot(x, y1)
yyaxis right
plot(x, y2)
Lines = findobj(gca, 'Type','line'); % 'Right' Line Is 'Line(1)', 'Left' Line is 'Line(1)'
RightX = Lines(1).XData;
RightY = Lines(1).YData;
LeftX = Lines(2).XData;
LeftY = Lines(2).YData;
This could be version-dependent. However this should work with recent MATLAB versions.
.
  댓글 수: 9
Star Strider
Star Strider 2022년 6월 23일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2022년 6월 22일
The two y-axes could have been drawn w/ the yyaxis function in which case there aren't actually two separate axes -- and it's not the axes you're tying to get, anyways, but the line handles...
While strongly agree w/ @Image Analyst that the way/time to do/have done this was when the figure was created and had the data to create it with instead -- but, there are occasional reasons such as the figure came from elsewhere that it's necessary to scrape the figure file --
hL=findobj(gcf,'type','line'); % return the line handle(s) of the figure
xData=arrayfun(@(hL)hL.XData.',hL,'UniformOutput',1); % return cell array of size number lines found
yData=arrayfun(@(hL)hL.YData.',hL,'UniformOutput',1);
  댓글 수: 1
dpb
dpb 2022년 6월 22일
hF=openfig('MyFig.fig');
hL=findobj(gcf,'type','line');
x=arrayfun(@(hL)hL.XData.',hL,'UniformOutput',0);
y=arrayfun(@(hL)hL.YData.',hL,'UniformOutput',0)
y =
2×1 cell array
{18×1 double}
{18×1 double}
>>
gets you there more directly...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by