Saving figures in MATLAB and string concatenation

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.

Advertisements

11 thoughts on “Saving figures in MATLAB and string concatenation

  1. Andrew

    This is exactly the answer to the problem I have been trying to solve for the past 3 hours. Thank you

  2. Martin

    Thanks a lot! I finally figured out how this works. I have looked for solution quite a long time 🙂

  3. aqui_c

    I have a problem and I need the figure to have some special domensions. I can do that when defining the figure, but when I use both saveas or print it is not preserved. Any thoughts?

  4. sc Post author

    aqui_c :

    I have a problem and I need the figure to have some special domensions. I can do that when defining the figure, but when I use both saveas or print it is not preserved. Any thoughts?

    Hi aqui_c, one thing I do is set the PaperPositionMode to auto. You can do this for the current figure (gcf) with:

    set(gcf, ‘PaperPositionMode’, ‘auto’);

    Alternatively, you can use the figure handle if it’s defined, in place of gcf. You have to set this for EVERY figure you create, if you want the dimensions to be preserved.

  5. Syed

    Hi, Can anyone help me w.r.to matlab issue.

    I am new to matlab.. I created spectrogram from a speech file…in that thru matlab.I selected 4 diff. formants. as u know…x axis has got time.. can i save this time. thru code…i can save the formant value.. pls. help. ibmmazher75@hotmail.com

  6. kunal

    hi…i need to resize the images to 512×512 and then save the image….m using imwrite to save but it saving it as blank images…my code is as below…plz help asap..

    a=imread(‘1.jpg’);
    b=double(a)+1;
    c=imresize(b, [512 512]);
    imwrite(c,’1f.jpg’);
    figure,imshow(a,[]);
    figure,imshow(c,[]);

  7. kunal

    hi…i need to resize the images to 512×512 and then save the image….m using imwrite to save but it saving it as blank images..,.my code is as below…plz help asap….

    a=imread(‘1.jpg’);
    b=double(a)+1;
    c=imresize(b, [512 512]);
    imwrite(c,’1f.jpg’);
    figure,imshow(a,[]);
    figure,imshow(c,[]);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s