Jd Baba
Jd Baba

Reputation: 6118

How to export data from the graph in Matlab?

I have a graph in matlab and I want to export the data from the graph. Is it possible to export data from the figure ?

I tried hard to export. Actually, I had exported data in the past but I forgot.

Any help is highly appreciated.

Thank you.

Upvotes: 0

Views: 5820

Answers (1)

meyumer
meyumer

Reputation: 5064

You can export x and y vectors from a figure (assuming the figure is a 2D plot for a single data set) by:

h = plot(1:10);
xVec = get(h,'XData');
yVec = get(h,'YData');

If you dont have the handle but the figure is open, then you can use gcf, gca as the handle for the current active figure or axis.

If you have multiple data sets (lines) in the figure you can get all of the associated data by:

lines = findobj(h, 'Type', 'line'); //h is the handle to the figure
nlines = length(lines);
points = cell(nlines,2);
for i = 1:nlines
    points{i,1} = get(lines(i),'XData');
    points{i,2} = get(lines(i),'YData');
end

Upvotes: 2

Related Questions