How can I store data from a plot in an ASCII text file when I have more than one line plotted in MATLAB?

9 views (last 30 days)
My axes contains more than one line object and I want to write the data from that axes containing the line objects into an ASCII text file. For example:
clc
clear
x = 1:10;
y1 = x;
y2 = sin(x);
y3 = cos(x);
plot(x,y1,...
x,y2,...
x,y3);
% clearing all existing varibles
clc
clear
% get handle to current axis
line_handle=findall(gca,'Type','line');
x=get(line_handle,'XData');
y=get(line_handle,'YData');
% saving file in ASCII text
save mydata.txt x y -ascii
When I execute the above code I get the following error:
%%%BEGIN WARNING MESSAGE%%%
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'x' not written to file.
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'y' not written to file.
%%%END WARNING MESSAGE %%%

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
When more than one handle is passed to the GET function, MATLAB stores the data in the form of cell arrays. For example, if you use:
y = get(h, YData);
and h is a scalar then y is returned as a vector. When h is a vector then y is returned as a cell array. These cell arrays cannot be stored as ASCII files using the SAVE function in MATLAB. To convert the cell arrays into matrices that can be stored in an ASCII file, add the following lines of code to the above example:
% converting date type from CELL to MAT
xx = cell2mat(x);
yy = cell2mat(y);
save mydata.txt xx yy -ascii
The complete code with the appended lines is as follows:
clc
clear
x = 1:10;
y1 = x;
y2 = sin(x);
y3 = cos(x);
plot(x,y1,...
x,y2,...
x,y3);
% clearing all existing varibles
clc
clear
% get handle to current axis
line_handle=findall(gca,'Type','line');
x=get(line_handle,'XData');
y=get(line_handle,'YData');
% converting date type from CELL to MAT
xx = cell2mat(x);
yy = cell2mat(y);
% saving file in ASCII text
save mydata.txt xx yy -ascii

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!