Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
getting the data out of Matlab fig

Subject: getting the data out of Matlab fig

From: Mahdieh

Date: 20 Apr, 2009 23:03:07

Message: 1 of 6

I have saved 50 2D simple graphs as a Matlab .fig(s).
Now I need to get the data out of the graphs, i.e. XY of the data points ...
I just need to know how to get the data for one graph.

Any help is appreciated,
Thanks,
-Mahdieh

Subject: getting the data out of Matlab fig

From: ImageAnalyst

Date: 21 Apr, 2009 00:01:21

Message: 2 of 6

I think it would be easier if you saved the data as .mat format files
with the save() function instead. Is that a possibility? Then you
just use the load() function and you're done - you've retrieved them.

Subject: getting the data out of Matlab fig

From: Phil Goddard

Date: 21 Apr, 2009 00:44:01

Message: 3 of 6


A .fig file is simply a .mat file with a different extension, so if you don't want to actually render the image you can do
>> s = load('filename.fig','-mat');
to load the data as a structure into the workspace.
The structure follows a similar heirarchy to that of handle graphics objects so you'll need to dig into it (mainly the 'children' field of the structure, and probably down several levels) to find the XData and YData fields.

Phil.

Subject: getting the data out of Matlab fig

From: Tingyue Gu

Date: 23 Apr, 2009 18:50:03

Message: 4 of 6

"Mahdieh" <mahdieh.emrani@capitalhealth.ca> wrote in message <gsiuva$sv8$1@fred.mathworks.com>...
> I have saved 50 2D simple graphs as a Matlab .fig(s).
> Now I need to get the data out of the graphs, i.e. XY of the data points ...
> I just need to know how to get the data for one graph.
>
> Any help is appreciated,
> Thanks,
> -Mahdieh

Hello for Tingyue Gu,
I wrote a free GUI utility called "MfigExtract" just to do that job. The target figure can contain multiple curves. x-axis ranges for the curves do not have to be the same! Output file is an .xls file (actually a text file with this extension) that can be opened using Excel or notepad. The data can be readily re-plotted in Excel.

This utility requires MATLAB R14 or above to run. I made the source code freely available. Search the web using keywords "extract x y data from matlab figure." If you cannot find it, find my home page using my name.

You can also save the following lines into an m-file to run it.

[filename1 pathname1]=uigetfile('*.fig','Select a figure file to extract x-y data');
if isequal(filename1,0) | isequal(pathname1,0); return; end %User pressed Cancel

[filename2, pathname2] = uiputfile('*.xls', 'Provide an Excel file name to save the x-y data');
if isequal(filename2,0) | isequal(pathname2,0)
    return
end %User pressed Cancel
    
if isempty(strfind(filename2,'.xls'));
    filename2=[filename2 '.xls'];
end

s = openfig(filename1); % open figure and get handle to figure

%get line handles
h = findobj(s,'Type','line'); %line is the type of your figure file.
if isempty(h)
line{1}='Your figure file does not contain x-y data.';
    line{2}='Did you accidentally load a wrong file?';
line{3}='Your can view your figures using the File menu.';
line{4}='Make sure you do not load a wrong file.';
uiwait(msgbox(line, 'No x-y data in your file','warn'));
close(s)
    return
end

x=get(h,'xdata');
y=get(h,'ydata');
[n dummy]=size(x); %n= number of x-y sets in the figure.

fid=fopen(fullfile(pathname2,filename2), 'wt');
fprintf(fid,'%s %s\n', 'x, y data extracted from ', [pathname1, filename1]);
fprintf(fid,'(A figure legend may generate extra x-y data sets with only one or two rows.)\n');
if n==1 %because x{i} becomes invalid symbol below if n=1
fprintf(fid,'%s\t %s\n', ' x1', ' y1');
fprintf(fid,'%g\t %g\n', [x;y]);
elseif n > 1

%x{1}=x1 series, y{1} is y1 series, etc.
%find out maximum x{i} length
maxsize=length(x{1});
for i=2:n
    if length(x{i}) > maxsize
        maxsize=length(x{i});
    end
end

%find out whether all the x series are the same
xseriessame=1;
for i=2:n
    if isequal(x{1},x{i})==0 %0 means not equal
        xseriessame=0;
    end
end

if xseriessame==1; %write x y1 y2... header
fprintf(fid,'All the x series in the figure are found to be the same.\n');
fprintf(fid,' x\t');
    for i=1:n
    fprintf(fid,' y%g\t',i);
    end
fprintf(fid,'\n'); %carriage return for header line

%Write each x,y pair horizontally in the output file
    for j=1:maxsize
        fprintf(fid,'%g\t',x{1}(j)); %x series are the same! Write x{1} for all
        for i=1:n
            fprintf(fid,'%g\t',y{i}(j));
        end
    fprintf(fid,'\n'); %carriage return at end of each row
    end
elseif xseriessame==0

%write x, y column header
for i=1:n
       fprintf(fid,' x%g\t y%g\t',i,i);
end
fprintf(fid,'\n'); %carriage return for header line

%Write each x,y pair horizontally in the output file
for j=1:maxsize
        for i=1:n
            if j > length(x{i}) %the x-y pair has ended earlier than others
                fprintf(fid,'\t \t'); %mark current x, y as empty
            elseif j <= length(x{i})
                fprintf(fid,'%g\t %g\t',x{i}(j),y{i}(j));
            end
        end
fprintf(fid,'\n'); %carriage return at end of each row
end

end %ending elseif xseriessame==0 statement
end %ending elseif n>1 statement

fclose(fid);
message{1}='Output from last run:';
message{3}=sprintf('File = %s',filename2);
message{5}=sprintf('Directory = %s',pathname2);
msgbox(message,'Attention','none');


 

Subject: getting the data out of Matlab fig

From: Tingyue Gu

Date: 28 Apr, 2009 17:25:07

Message: 5 of 6

"Tingyue Gu" <gu@ohio.edu> wrote in message <gsqd8r$ke4$1@fred.mathworks.com>...
> "Mahdieh" <mahdieh.emrani@capitalhealth.ca> wrote in message <gsiuva$sv8$1@fred.mathworks.com>...
> > I have saved 50 2D simple graphs as a Matlab .fig(s).
> > Now I need to get the data out of the graphs, i.e. XY of the data points ...
> > I just need to know how to get the data for one graph.
> >
> > Any help is appreciated,
> > Thanks,
> > -Mahdieh
>
> Hello for Tingyue Gu,
> I wrote a free GUI utility called "MfigExtract" just to do that job. The target figure can contain multiple curves. x-axis ranges for the curves do not have to be the same! Output file is an .xls file (actually a text file with this extension) that can be opened using Excel or notepad. The data can be readily re-plotted in Excel.
>
> This utility requires MATLAB R14 or above to run. I made the source code freely available. Search the web using keywords "extract x y data from matlab figure." If you cannot find it, find my home page using my name.
>
> You can also save the following lines into an m-file to run it.
>
> [filename1 pathname1]=uigetfile('*.fig','Select a figure file to extract x-y data');
> if isequal(filename1,0) | isequal(pathname1,0); return; end %User pressed Cancel
>
> [filename2, pathname2] = uiputfile('*.xls', 'Provide an Excel file name to save the x-y data');
> if isequal(filename2,0) | isequal(pathname2,0)
> return
> end %User pressed Cancel
>
> if isempty(strfind(filename2,'.xls'));
> filename2=[filename2 '.xls'];
> end
>
> s = openfig(filename1); % open figure and get handle to figure

s = openfig(filename1)
should be replaced by
s = openfig(fullfile(pathname1,filename1))

Cheers!

Subject: getting the data out of Matlab fig

From: Tingyue Gu

Date: 28 Apr, 2009 18:27:01

Message: 6 of 6

OK. I made minor changes.

Figure legends may add some very short data columns to the output Excel file. In the code below, I removed legends before reading x, y data. You can get the GUI version of MfigExtract by searching the web using keywords "how to extract x y data from MATLAB figure."

Save the texts below as extract.m and run it.

[filename1 pathname1]=uigetfile('*.fig','Select a figure file to extract x-y data');
if isequal(filename1,0) | isequal(pathname1,0); return; end %User pressed Cancel

[filename2, pathname2] = uiputfile('*.xls', 'Provide an Excel file name to save the x-y data');
if isequal(filename2,0) | isequal(pathname2,0)
    return
end %User pressed Cancel
    
if isempty(strfind(filename2,'.xls'));
    filename2=[filename2 '.xls'];
end

s=hgload(fullfile(pathname1,filename1)); % open figure and get handle to figure
delete(legend); %legends add extra data to output. Remove legends

%get line handles
h = findobj(s,'Type','line'); %line is the type of your figure file.
if isempty(h)
line{1}='Your figure file does not contain x-y data.';
    line{2}='Did you accidentally load a wrong file?';
line{3}='Your can view your figures using the File menu.';
line{4}='Make sure you do not load a wrong file.';
uiwait(msgbox(line, 'No x-y data in your file','warn'));
close(s)
    return
end

x=get(h,'xdata');
y=get(h,'ydata');

close(s) %close figure with legends removed
s=hgload(fullfile(pathname1,filename1)); %show original figure with legends

[n dummy]=size(x); %n= number of x-y sets in the figure.

fid=fopen(fullfile(pathname2,filename2), 'wt');
fprintf(fid,'%s %s\n', 'x, y data extracted from ', [pathname1, filename1]);
fprintf(fid,'(A figure legend may generate extra x-y data sets with only one or two rows.)\n');
if n==1 %because x{i} becomes invalid symbol below if n=1
fprintf(fid,'%s\t %s\n', ' x1', ' y1');
fprintf(fid,'%g\t %g\n', [x;y]);
elseif n > 1

%x{1}=x1 series, y{1} is y1 series, etc.
%find out maximum x{i} length
maxsize=length(x{1});
for i=2:n
    if length(x{i}) > maxsize
        maxsize=length(x{i});
    end
end

%find out whether all the x series are the same
xseriessame=1;
for i=2:n
    if isequal(x{1},x{i})==0 %0 means not equal
        xseriessame=0;
    end
end

if xseriessame==1; %write x y1 y2... header
fprintf(fid,'All the x series in the figure are found to be the same.\n');
fprintf(fid,' x\t');
    for i=1:n
    fprintf(fid,' y%g\t',i);
    end
fprintf(fid,'\n'); %carriage return for header line

%Write each x,y pair horizontally in the output file
    for j=1:maxsize
        fprintf(fid,'%g\t',x{1}(j)); %x series are the same! Write x{1} for all
        for i=1:n
            fprintf(fid,'%g\t',y{i}(j));
        end
    fprintf(fid,'\n'); %carriage return at end of each row
    end
elseif xseriessame==0

%write x, y column header
for i=1:n
       fprintf(fid,' x%g\t y%g\t',i,i);
end
fprintf(fid,'\n'); %carriage return for header line

%Write each x,y pair horizontally in the output file
for j=1:maxsize
        for i=1:n
            if j > length(x{i}) %the x-y pair has ended earlier than others
                fprintf(fid,'\t \t'); %mark current x, y as empty
            elseif j <= length(x{i})
                fprintf(fid,'%g\t %g\t',x{i}(j),y{i}(j));
            end
        end
fprintf(fid,'\n'); %carriage return at end of each row
end

end %ending elseif xseriessame==0 statement
end %ending elseif n>1 statement

fclose(fid);
message{1}='Output from last run:';
message{3}=sprintf('File = %s',filename2);
message{5}=sprintf('Directory = %s',pathname2);
msgbox(message,'Attention','none');

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us