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

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 Comments

You forgot to attach MyFig.fig.
Why are you trying to get it from a fig file in the first place? Don't you have the original data? If it has to be transferred via a disk file, I'd recommend using save to write a .mat file instead of a .fig file.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
@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.
@Image Analyst thanks for your reply.
I have attached the file now. unfortunatly, I dont have the original data.

Sign in to comment.

 Accepted Answer

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 Comments

Thanks for your help, your code works will with me.
But, not with my figuer.
by the way i have attached the fig in the main post.
It works correctly when I run it with your figure —
F = openfig('MyFig.fig');
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;
figure
subplot(2,1,1)
semilogx(LeftX, LeftY, '.-b')
grid
xlim([1E1 1E5])
xlabel('Frequency (Hz)')
ylabel('Impedance Magnitude [\Omega]')
title('Left Y-Axis Data')
subplot(2,1,2)
semilogx(RightX, RightY, '.-r')
grid
xlim([1E1 1E5])
xlabel('Frequency (Hz)')
ylabel('Impedance Phasee [°]')
title('Right Y-Axis Data')
sgtitle('Recovered Data Plots')
Producing —
The only line I added to my previous code was the openfig call, and the figure and related subplot calls to replot the data. The only deletions I made were the creation of my test figure.
.
Thanks alot for your time and effort,
it works will now, but i dont really know what was the problem, such that i cant make the same for all the files, as an example the attached one.
This time, it’s an errorbar plot, not a line. They are not the same.
Incorporate these changes:
F = openfig('MyFig1.fig');
Ax = F.CurrentAxes;
PlotType = Ax.Children.Type;
Lines = findobj(Ax, 'Type',PlotType); % 'Right' Line Is 'Line(1)', 'Left' Line is 'Line(1)'
This works for both figure objects presented to it so far.
The rest of my previous code then works correctly.
This is the best I can do to make my code robust as possible.
.
I wish TMW would quit doing this -- making more and more of these specialized objects that are, fundamentally just axes/lines/etc...it is more and more convoluted to write code to handle the special cases they create.
@dpb — This project has certainly been an education for me in that regard!

Sign in to comment.

More Answers (1)

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 Comment

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...

Sign in to comment.

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!