Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: getting the data out of Matlab fig
Date: Thu, 23 Apr 2009 18:50:03 +0000 (UTC)
Organization: Ohio University
Lines: 118
Message-ID: <gsqd8r$ke4$1@fred.mathworks.com>
References: <gsiuva$sv8$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1240512603 20932 172.30.248.35 (23 Apr 2009 18:50:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 23 Apr 2009 18:50:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 335850
Xref: news.mathworks.com comp.soft-sys.matlab:535043


"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');