2012-07-19

Data extraction from MATLAB figure


Sometimes, we create function, which makes calculations and build a diagrams. When a function is finish calculations, all data removes from memory, and we have only figure with diagram finally. If you need quickly export \(XY\)-data from figure to MATLAB workspace, you can use following short script
fig_handle = gcf(0,’Children’); % get handle to all figures
you can use
fig_handle = gcf; % get handle to current (active) figure window only
then
obj_handle = gco(fig_handle(k)); % get a handle to the current object at figure #k
also it is possible to use
obj_handle = get(fig_handle,’Children’); % get a list of handles to all objects @ figure #k
graph_handle = get(obj_handle,’Children’); % handles for each diagram
x = get(graph_handle(n),’XData’); % read x-coordinate for diagram #n
y = get(graph_handle(n),’YData’); % read y-coordinate for diagram #n
If you type get(graph_handle(n)) you can see all structure of the class.

Example:
X = 0:100;
Y1 = exp(X/100).*cos(X/50);
Y2 = exp(X/100).*cos(X/100);
plot(X,Y1,’r’,X,Y2,’b’);
f = gcf;
h = gco(f);
d = get(h,’Children’);
x = get(d(1),’XData’);
y1 = get d(1),’YData’);
y2 = get d(2),’YData’);

No comments: