"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. xaxis 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 replotted 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 mfile to run it.
[filename1 pathname1]=uigetfile('*.fig','Select a figure file to extract xy 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 xy 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 xy 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 xy data in your file','warn'));
close(s)
return
end
x=get(h,'xdata');
y=get(h,'ydata');
[n dummy]=size(x); %n= number of xy 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 xy 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 xy 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');
