File Exchange Pick of the Week

Grab a screen shot from within MATLAB

Posted by Brett Shoelson,

Brett's Pick this week is the ScreenCapture, by Yair Altman.

Those of you who follow the happenings on MATLAB Central (in general) are likely already familiar with Yair's contributions. Over the years, Yair has provided great value, through both the File Exchange and the Newsgroup. In particular, Yair is the Yoda of undocumented MATLAB, and has, in fact, written a great textbook on the subject.

We generally try to avoid Picking File Exchange submissions that rely on undocumented MATLAB to get things done; there's simply no guarantee that what works in this version of MATLAB will continue to work in the next. Your mileage may vary.

That said, today's featured file is both very useful, and reliant only on documented aspects of the MATLAB language.

So what does ScreenCapture do? You can provide the handle to a figure or an axes--indeed, to any valid MATLAB handle, including handles to the desktop or to uicontrols-- and easily capture an image from it:

L = 40*membrane(1,25);

logoFig = figure('Color',[0 0 0]);

logoAx = axes('parent',logoFig);

surf(L);

title('Original','color','r','fontsize',24)

figcap = screencapture(logoFig);

axcap = screencapture(logoAx);

figure

subplot(1,2,1)

imshow(figcap)

title('Figure Cap')

subplot(1,2,2)

imshow(axcap)

title('Axes Cap')

Additionally, you can specify the 'position' of a capture to grab a portion of a window, or you can specify the name of a file to which you can directly write the image information.

I particularly like Yair's documentation, and his attention to detail. He went to a lot of effort (much of it at my suggestion!) to accommodate image handles in their native units so that this works on images much like imcrop does. I also like the interactive capabilities of the tool; if you don't specify a handle or position, you will be prompted to draw a region of interest with a rubberband box. And, Yair has "teased" us with some "Future enhancements," including enabling rbbox capture from non-figures (it will be nice to capture from anywhere, including, for instance, the MATLAB editor!), and capture directly to the system clipboard!

As always, comments to this blog post are welcome. Or leave a comment for Yair here.


Get the MATLAB code

Published with MATLAB® 7.14

Comments are closed.

These postings are the author's and don't necessarily represent the opinions of MathWorks.