An economics graduate student and friend of mine recently asked Google out of frustration, “How the hell do I save a plot in MATLAB?” Google, being the unfeeling robot that it is, did not empathize with his question, but many of us certainly can. The useful commands here are ‘print’ and ‘saveas’. Here is a qnd guide to them in the ways I use them, with lots of examples.
I always save the raw .fig figures so that I can edit plots in MATLAB without having to regenerate them. To do so, we will use the ‘saveas’ command in the following way:
saveas(gcf, '~/Desktop/fig1.fig', 'fig');
The ‘saveas’ command in this functional form is taking 3 arguments. The first argument in this example is ‘gcf’, which tells saveas to “get current figure” handle. This is the active or topmost figure window. This can be replaced with a figure handle reference. If you don’t know what that means, then look it up! Figure handles are very useful! The second argument is simply the full path name of our file. Finally, the third argument tells MATLAB to save in the file format .fig. Note the quotes. Respect the quotes. Abide by the quotes.
To save figures in png or eps format, I use the command ‘print’, somewhat non-intuitively. In MATLAB, the print command can print to either a physical device (a printer) or print to a file. We are using the latter in this case, with the example of the eps.
print(gcf, '-depsc', '~/Desktop/fig1.eps');
Again, ‘print’ in the functional form takes 3 arguments here. The first is ‘gcf’, much like in saveas. The second is a device specification. The leading ‘d’ stands for device, I think, and here it tells the print command that the device is a color eps file. Finally, the last argument is the path, much like the second argument of the saveas command. It’s important to note that, as of MATLAB 7.6, there is no native support for embedded fonts in eps files. Journals and possibly collaborators will yell at you. Send a worded letter to the fine folks at The MathWorks in Natick, MA, if you feel this should be included. Alternatively, there is a package on MATLAB Central that may be helpful in this.
As always, other features of these two commands can be found by using the MATLAB help system, either ‘help saveas’ or ‘help print’.
One quick note about path names: throughout MATLAB, strings and variables for strings can be concatenated on the fly using the square bracket notation. This comes in handy with long path names and automating file saving in a script. The notation can be used in the following way:
ext = '.fig';
path = '~/Desktop/';
file_name = 'fig1';
The saveas command now becomes:
saveas(gcf, [path, file_name, ext], 'fig');
Note that there are no single quotes around the variables; the strings in the variables automatically get substituted for the concatenation. Again, variables and strings can be combined here:
saveas(gcf, ['~/', file_name, ext], 'fig');
will save the figure now in the home directory. Just know that this option exists — it is useful for me quite often. Hopefully all of this is useful to someone out there in the world, too.