kennethkam.com

MATLAB Tricks

MATLAB has become part of my life recently. As I used it more often, I realised that there were a few things I needed to do that required scripting in MATLAB. For example, I wanted to create same-sized .eps figures for my research project, or be able to reorder the legend for graphs.

Creating same-sized figures

Visiting the MATLAB documentation solved my question.

set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperPosition', [0 0 4 2]);

PaperPosition follows this format:

[left bottom width height]

Finally, save the file as .eps:

print -despc output.eps

This can be repeated reliably, which will guarantee consistently-sized figures.

Reordering the legend

Visiting this forum thread helped.

% get the figure handle
h = 1 % or whatever fig
% get the axes handle
a = get(h, 'CurrentAxes')
% get the handles for children of the axes -- these are the data series handles
c = get(a, 'Children')
% generate a legend command using these "children"
legend(c([1 3 5]), 'label for data 1', 'label for 3', 'label for 5')

Remove all variables

Sometimes it is nice to remove all the variables using the command line:

clear all

Comments

  • There are no comments.

Add a Comment