Extracting data from matlab figure into .txt file

20 次查看(过去 30 天)
can someone plz tell me how to extract data from matlab figure to .txt file?
i tried the follwing code
%clear all;
%close all;
%clc
h=openfig('MCBNew.fig');
h=findobj(gca,'Type','Line');
x=get(h,'Xdata');
y=get(h,'Ydata');
A=[];
A(:,1)=y;
A(:,2)=x;
dlmwrite('data.txt',A,',')
it is giving the following error.
Conversion to double from cell is not possible.
Error in ED (line 13)
A(:,1)=y;
the figure is attached here.
  2 个评论
Walter Roberson
Walter Roberson 2019-7-27
Is it correct that you want all of the x and y data to appear as one continuous column for each, with no indication of where the breaks between lines are?
Fakhere Alam
Fakhere Alam 2019-7-28
yes sir. what i want is one column of x-axis and one column of y-axis

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2019-7-27
How on earth was that figure created??? In a loop plotting a new complete line for every iteration of each point, apparently...
Your code for your figure returns
>> h=findobj(gca,'Type','Line');
>> whos h
Name Size Bytes Class Attributes
h 76x1 8 matlab.graphics.chart.primitive.Lin
so there are 76 (count 'em!) lines making up the figure, not just one.
Exploring what's up with that we find--
>> whos x
Name Size Bytes Class Attributes
x 76x1 31920 cell
>> x(1:5)
ans =
5×1 cell array
{1×76 double}
{1×75 double}
{1×74 double}
{1×73 double}
{1×72 double}
>> x(end)
ans =
1×1 cell array
{[0]}
>>
and the same thing with the y. In short, "don't do that!" and you won't have so much trouble going forward. For this particular figure, after the figure is loaded and you have the line handle array,
delete(h(2:end)) % get rid of the 75 unneeded partial lines drawn in the figure
and then resave it if need it going forward.
To retrieve the data, having done that you can either--
hAx=gca; % axes handle; remember we cleaned the figure up by deleting extraneous lines
hL=hAx.Children; % handle to the one remaining line
x=hL.XData; x=x(:); % retrieve x,y data, ensure are column vectors
y=hL.YData; y=y(:);
csvwrite('data.txt',[x y])
or just address h(1) only in your original figure ignoring the other 75 line handles. I'd do the former without question (and strongly suggest fixing the original code that created the figure or asking its author to do so if you have to do this more than just this once.
  2 个评论
Fakhere Alam
Fakhere Alam 2019-7-28
sir,
i am begginer in Matlab Programming. what i want is to extract the x and y data from the figure and export it to excel.
dpb
dpb 2019-7-28
编辑:dpb 2019-7-28
So, use xlswrite instead...the above extracts the data and writes to .csv file as did your original; did you try it?
Or, of course, you can open a .csv file in Excel...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by