Matlab Startup (.m)

For those of you who use MATLAB on a regular basis, I'd like to share a trick I use to simplify my life considerably. One of the things I find myself constantly doing is resizing text on figures. I'm always increasing text to 14-16 point font and setting the face to bold. This is okay if you have one figure to make, but when you need to do it to every figure, and there are 10-20 staring back at you?

First you could start by setting the default figure - you can address its properties using 0 (zero) as the figure handle (see below for more on handles). But there's an even easier way! Using startup.m, we can set the defaults so that when MATLAB starts, the font size / face for the graphs will be set. When MATLAB loads, it looks for a script called "startup.m" which it will then execute before giving you control. By placing our overrides for the default figures here, we don't have to enter them in by hand every time we start MATLAB.

Here's how to do it: Open a new script and save it as startup.m in one of the paths MATLAB searches. You can find the paths by going to File -> Set Path. Something like "My Documents\MATLAB\" would be an example path for a Windows user.
Now for the contents of the script. Here's mine;
% startup.m
% Called at MATLAB startup.
% Sets defaults for figures (use 0 for figure handle)
% Adjust figure defaults for better font read-ability

set(0,'DefaultFigureColor',     'white',...
      'DefaultAxesColor',       'white',...
      'DefaultTextFontSize',    16,...
      'DefaultAxesFontSize',    14,...
      'DefaultTextFontWeight',  'bold',...
      'DefaultAxesFontWeight',  'bold');

% Load up my lovely custom colormap that uses black as middle value.
load('MyColormaps');
set(0,'DefaultFigureColormap',mycmap);
clear;
Let's go over each command;
set(0,'DefaultFigureColor', 'white',...,'DefaultTextFontSize',16,'DefaultAxesFontSize',14,...);
If you're not familiar with the get() and set() commands you really should be. These are a great instant-reference when you need something. The syntax is usually get(FigureHandle) and set(FigureHandle,Property,Value). If you're working on only one figure, you can use get(gcf) (get all values for the current figure), a list of properties and values will be displayed to you. If you are working with multiple figures, it is usually best to assign each one a unique handle, so you don't get confused. For example; "myh1 = figure;" assigns the next figure created a handle of "myh1", then you can use get(myh1) and set(myh1,...) to get/set values of that figure.

Now the second group of commands;
load('MyColormaps');
set(0,'DefaultFigureColormap',mycmap);
clear;
This loads a file called "MyColormaps" within which I have saved a colormap, "mycmap" (creative, I know). You don't need this unless you are (like I am) highly unsatisfied with the default colormaps. I'll go over in the future how to make custom colormaps, but you can leave that out of your startup.m for now. Finally, I clear the variables "MyColormaps" has loaded in my workspace (I like having an empty workspace on startup).

I'm always interested in hearing about your tips and tricks when it comes to simplifying life in MATLAB / Mathematica, etc. Give a shout back if you have something you'd like to share.

No comments:

Post a Comment