Saturday, July 17, 2010

~ Fiddling with Figures

Salam alaik,

People've got an awesome practice of saving figures in the simulation script just after results are finalized.

Its a jolly good habit for all i can tell, since it pretty much prevents a lot of future headache.

No rocket science there..
The function to save a MATLAB figure file "saveas"; can save in several formats including fig (matlab figure), bmp (bitmap - though not recommended), ai (adobe illustrator), psc2 (colored post-script - recommended for latex), so on n so forth.

Fig files are useful in another sense, i.e., the data is automatically secured with it. n i'll show you in a minute -inshAllah- how data could be extracted from the saved fig files;
  1. Open the saved .fig file using open function. e.g., open abc.fig
  2. Use gcf to get the current figure pointer/handler and save it in a variable, e.g., h = gcf
  3. Use get command to get hold of the children of the gcf, H = get(h, 'children')
  4. Finally, search for the handle of the child which containts all the plots.
  5. Suppose H(2) is the child with all the plots, then use H2 = get(H(2), 'children')
  6. Now use set(H2(i)), where i = 1 to number of plots, to list all the properties and change their values.
  7. Similarly, use the get(H2(i)) to fetch the values of all the properties.


For example, the script below extracts data from the plots.


clear all;
% open the .fig file
open test.fig; 
% get the current graph handle
h = gcf; 
% get the children of the figure file
H = get(h, 'Children');
% For this specific case the 2nd child has all the plots
H2 = get(H(2), 'Children');

Y = [];
for i = 1:length(H2)
% get the data of y-axis
    Y = [Y; get(H2(i), 'YData')];
end

Here is a test.fig, i fiddled with using the above script;


..enjoy.
n happy scripting!

~*~*~*~*~~*~*~*~*~
blog comments powered by Disqus