### Templates, Why Matlab is the Awesomeness! (Part 3)

Powerpoint is ok for making basic graphs, as I explained yesterday. I still prefer to hardcode in LaTeX, but you can use something like Inkscape instead, which is open source and works with EPS files and is full vector graphics. When you need to plot data though, that’s where Matlab comes in handy.

I hear from a lot of my colleagues that Matlab is too cumbersome to make consistent plots for their thesis’s (is that the correct plural of thesis?). If you didn’t take the time to make a proper template, then yes, it is too cumbersome to make consistent plots. But if you take the time to build a proper template, like yesterday with Powerpoint, you will be able to churn out nice graphs with relative ease. Let’s face it, if you’re working with large datasets, you’re using Matlab anyway…

**Basic Figure Setup**

I’m going to assume you already have a plot on the screen. Prior to changing the figure properties, you an actual figure to work on. To build a test figure, I’m going to assume we have two datasets ( X and Y) that we want to plot with respect to time (t).

>> h1 = plot( t , X);

>> h2 = line( t , Y);

I typically work with colored figures so I want to change X and Y to blue and red. This is why I plotted them using separate line commands. If you want to change the MarkerStyle, and MarkerSize, and LineWidth, here’s your chance.

>> set( h1 , ‘Color’ , [0 0 1] );

>> set( h1 , ‘Color’ , [1 0 0] );

The next step is to change the axes to fit the data. So if

*t*is from 0 to 50 seconds and*X*and*Y*both range from -10 Volts to 10 Volts, then you need to set the axes to these limits. After setting the axes, it’s good to go ahead and make sure you have enough tick marks and label the axes.>> axis([0 50 -10 10]);

>> set( gca , ‘XTick’ , [0 : 5 : 50] );

>> set( gca , ‘YTick’ , [-10 : 2 : 10] );

>> xlabel( gca , ‘Time [s]’ );

>> ylabel( ‘Signal Noise [V]’ );

So now you have your figure with the right axes, tick marks, and labels, with the lines the colors that you want. But the figure size is scaled to fit your screen, and your fonts and font sizes are all off. Scaling to a particular width and changing font attributes are generally routine tasks that need to be done for all figures for publications. So I make a separate M-file called “Figure2c.m”and put it in a Path folder. Then I can just run it below my figure when I’m plotting my data.

>> Figure2c

Matlab will then just execute the commands in the file called “Figure2c”. The 2c stands for two columns. I have other ones for log plots and wider figures as well.

**Start with the Figure Properties.**

This sounds eerily like yesterday. But if you know you only have 8.5 cm (sorry but I work in metric) of width to work with, you should start by making your figure only 8.5 cm wide. In you Figure2c file, you want to put the following text:

>> function Figure2c

>> set( gcf , ‘PaperUnits’ , ‘Centimeters’ ,…

‘Units’ , ‘Centimeters’ ,…

‘PaperOrientation’ , ‘Portrait’ ,…

‘PaperPositionMode’ , ‘auto’ ,…

‘Position’ , [2 1 8.5 4.5] );

>> PAPER = get( gcf , ‘Position’ );

>> set ( gcf , ‘PaperSize’ , [PAPER(3) PAPER(4)] );

The

*function*command means the file runs when called from another file. The next block changes the current figure properties by*grabbing the current figure*(GCF). Matlab, by default, plots things relative to your screen size, which you need to change to real units. So I’ve switched them, and all other units in the figure to centimeters. Then I change the paper position so it start 2 cm from the left edge of the computer screen, 1 cm from the bottom and the figure is 8.5 cm wide by 4.5 high. I find this aspect ratio quite nice for figures. You can go higher if you want. The next two lines get the figure wide and change the maximum paper size to the exact figure size. When you save your figure, you won’t have any excess white space around the figure because of the paper. This is critical for saving EPS figures.**Change the Axis Properties**

After making the figure the right size, the next trick is to make the axes essentially fill the figure. This takes some trial and error (good thing I’ve done it for you). Like the figure properties, you need to change the current axis properties to work with your real units. So use the following…

>> set( gca , ‘Units’ , ‘centimeters’ ,…

‘Position’ , [1.3 1.1 6.9 3] ,…

‘FontSize’ , 8 ,…

‘FontName’ , ‘NewCenturySchoolbook’ );

This changes the axis units to centimeters. Now you need to position the axes within the figure box, and include space for the axis labels and tick marks. This where some trial and error can occur. I’ve found for most figures, the actual plot is 6.9 cm wide by 3 cm high and should start about 1.3 cm from the left edge of the figure window and 1.1 cm up from the bottom of the figure window. Then I change the fonts to 8 pt, New Century Schoolbook.

The last step for this part is to change the axis labels to the right font, fontsize, and color (if needed). To do this you need to first copy the previous label name.

>> holderX = get( gca , ‘xlabel’ );

>> holderY = get( gca , ‘ylabel’ );

Then you need to reset it based on the string from your original plot. That’s what the first attribute is below. The next two change the font size to 9 and the font to New Century Schoolbook to match the other properties.

>> xlabel( get( holderX , ‘String’ ) ,…

‘FontSize’ , 9 ,…

‘FontName’ , ‘NewCenturySchoolBook’);

>> ylabel( get( holderY , ‘String’ ) ,…

‘FontSize’ , 9 ,…

‘FontName’ , ‘NewCenturySchoolBook’);

**And there’s so much more you can do**

Using a template like this, I add gridlines in a light grey, but still have axes that are plotted in black (which is somewhat tricky to do). Also, you can add a format for you legend to make sure you have the right font and font size. You can do two Y-axis plots fairly easily with this and do color coding, etc etc.

**The last step: Exporting**

So now you have your beautiful plot and you need to save it. Since you’re running this in M-files anyway, you might as well save the figure via the M-file as well. I export everything to PNG (portable network graphics) which is good for bringing it into Powerpoint. For papers, I use EPS figures with is a vector format. The two commands I use are based on *print*which prints the current figure.

>> print -r600 -dpng -loose filename.png

>> print -r600 -depsc -loose filename.eps

-r600 makes sure I have 600 DPI on the figures. -dpng/-depsc are the switches for which print driver to use. Matlab has both by default. -loose is very important. Don’t forget that. That ensures the paper size and position that you specified in the beginning will be used for the bounding box. That’s crucial for importing EPS figures and working with LaTeX. The last bit is the filename.

There you have it. It’s a basic template but it works. If you have any questions or want some more tips on Matlab plotting, let me know.