Extracting data from matlab figure into .txt file

조회 수: 32 (최근 30일)
Fakhere Alam
Fakhere Alam 2019년 7월 27일
편집: dpb 2019년 7월 28일
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에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by