{"id":3771,"date":"2012-08-10T08:05:34","date_gmt":"2012-08-10T13:05:34","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3771"},"modified":"2022-11-24T01:45:15","modified_gmt":"2022-11-24T06:45:15","slug":"grab-a-screen-shot-from-within-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2012\/08\/10\/grab-a-screen-shot-from-within-matlab\/","title":{"rendered":"Grab a screen shot from within MATLAB"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction><\/introduction>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24323-screencapture\">ScreenCapture,<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/27420\">Yair Altman<\/a>.\r\n   <\/p>\r\n   <p>Those of you who follow the happenings on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/\">MATLAB Central<\/a> (in general) are likely already familiar with Yair's contributions. Over the years, Yair has provided great value, through\r\n      both the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=authorid:27420\">File Exchange<\/a> and the <a>Newsgroup<\/a>. In particular, Yair is the Yoda of undocumented MATLAB, and has, in fact, written a great <a href=\"https:\/\/undocumentedmatlab.com\/books\">textbook<\/a> on the subject.\r\n   <\/p>\r\n   <p>We generally try to avoid Picking File Exchange submissions that rely on undocumented MATLAB to get things done; there's simply\r\n      no guarantee that what works in this version of MATLAB will continue to work in the next. Your mileage may vary.\r\n   <\/p>\r\n   <p>That said, today's featured file is both very useful, and reliant only on documented aspects of the MATLAB language.<\/p>\r\n   <p>So what does ScreenCapture do? You can provide the handle to a figure or an axes--indeed, to any valid MATLAB handle, including\r\n      handles to the desktop or to uicontrols-- and easily capture an image from it:\r\n   <\/p>\r\n   <p><tt>L = 40*membrane(1,25);<\/tt><\/p>\r\n   <p><tt>logoFig = figure('Color',[0 0 0]);<\/tt><\/p>\r\n   <p><tt>logoAx = axes('parent',logoFig);<\/tt><\/p>\r\n   <p><tt>surf(L);<\/tt><\/p>\r\n   <p><tt>title('Original','color','r','fontsize',24)<\/tt><\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/screengrab0.png\"> <\/p>\r\n   <p><tt>figcap = screencapture(logoFig);<\/tt><\/p>\r\n   <p><tt>axcap = screencapture(logoAx);<\/tt><\/p>\r\n   <p><tt>figure<\/tt><\/p>\r\n   <p><tt>subplot(1,2,1)<\/tt><\/p>\r\n   <p><tt>imshow(figcap)<\/tt><\/p>\r\n   <p><tt>title('Figure Cap')<\/tt><\/p>\r\n   <p><tt>subplot(1,2,2)<\/tt><\/p>\r\n   <p><tt>imshow(axcap)<\/tt><\/p>\r\n   <p><tt>title('Axes Cap')<\/tt><\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/screengrab1.png\"> <\/p>\r\n   <p>Additionally, you can specify the 'position' of a capture to grab a portion of a window, or you can specify the name of a\r\n      file to which you can directly write the image information.\r\n   <\/p>\r\n   <p>I particularly like Yair's documentation, and his attention to detail. He went to a lot of effort (much of it at my suggestion!)\r\n      to accommodate image handles in their native units so that this works on images much like <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/imcrop.html\"><tt>imcrop<\/tt><\/a> does.  I also like the interactive capabilities of the tool; if you don't specify a handle or position, you will be prompted\r\n      to draw a region of interest with a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/ref\/rbbox.html\">rubberband box<\/a>. And, Yair has \"teased\" us with some \"Future enhancements,\" including enabling rbbox capture from non-figures (it will be\r\n      nice to capture from anywhere, including, for instance, the MATLAB editor!), and capture directly to the system clipboard!\r\n   <\/p>\r\n   <p>As always, <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3771#respond\">comments to this blog post<\/a> are welcome. Or leave a comment for Yair <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24323-screencapture#comments\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_fb9f84a64ed241129c757b4b5608fe86() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='fb9f84a64ed241129c757b4b5608fe86 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' fb9f84a64ed241129c757b4b5608fe86';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Brett Shoelson';\r\n        copyright = 'Copyright 2012 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_fb9f84a64ed241129c757b4b5608fe86()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.14<br><\/p>\r\n<\/div>\r\n<!--\r\nfb9f84a64ed241129c757b4b5608fe86 ##### SOURCE BEGIN #####\r\n%% Grab a screen shot from within MATLAB\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24323-screencapture ScreenCapture,> by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/27420 Yair Altman>.\r\n\r\n%% \r\n% Those of you who follow the happenings on\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/ MATLAB Central>\r\n% (in general) are likely already familiar with Yair's\r\n% contributions. Over the years, Yair has provided great\r\n% value, through both the <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=authorid:27420 File Exchange> and the\r\n% <http:\/\/search_results?search_term=&search_tag=&search_author=yair+altman&search_subject=&search_tagger=&dur=365&sort_by=date&sort_dir=dn\r\n% Newsgroup>. In particular, Yair is the Yoda of undocumented MATLAB, and has, in fact, written a great <https:\/\/undocumentedmatlab.com\/books textbook> on the subject. \r\n\r\n%% \r\n% We generally try to avoid Picking File Exchange\r\n% submissions that rely on undocumented MATLAB to get things\r\n% done; there's simply no guarantee that what works in this\r\n% version of MATLAB will continue to work in the next. Your\r\n% mileage may vary.\r\n\r\n%%\r\n% That said, today's featured file is both very useful, and reliant only on documented aspects of the MATLAB language.\r\n\r\n%%\r\n% So what does ScreenCapture do? You can provide the handle\r\n% to a figure or an axesREPLACE_WITH_DASH_DASHindeed, to any valid MATLAB\r\n% handle, including handles to the desktop or to\r\n% uicontrolsREPLACE_WITH_DASH_DASH and easily capture an image from it:\r\n\r\n%%\r\n% |L = 40*membrane(1,25);|\r\n%%\r\n% |logoFig = figure('Color',[0 0 0]);|\r\n%%\r\n% |logoAx = axes('parent',logoFig);|\r\n%%\r\n% |surf(L);|\r\n%%\r\n% |title('Original','color','r','fontsize',24)|\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/screengrab0.png>>\r\n% \r\n%%\r\n% |figcap = screencapture(logoFig);|\r\n%%\r\n% |axcap = screencapture(logoAx);|\r\n%%\r\n% |figure|\r\n%%\r\n% |subplot(1,2,1)|\r\n%%\r\n% |imshow(figcap)|\r\n%%\r\n% |title('Figure Cap')|\r\n%%\r\n% |subplot(1,2,2)|\r\n%%\r\n% |imshow(axcap)|\r\n%%\r\n% |title('Axes Cap')|\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/screengrab1.png>>\r\n% \r\n%%\r\n% Additionally, you can specify the 'position' of a capture\r\n% to grab a portion of a window, or you can specify the name\r\n% of a file to which you can directly write the image\r\n% information. \r\n\r\n%%\r\n% I particularly like Yair's documentation, and his\r\n% attention to detail. He went to a lot of effort (much of\r\n% it at my suggestion!) to accommodate image handles in\r\n% their native units so that this works on images much like\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/images\/ref\/imcrop.html |imcrop|> does.  I also like the interactive capabilities\r\n% of the tool; if you don't specify a handle or position,\r\n% you will be prompted to draw a region of interest with a\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/ref\/rbbox.html rubberband box>.\r\n% And, Yair has \"teased\" us with some \"Future enhancements,\"\r\n% including enabling rbbox capture from non-figures (it will be nice to capture from\r\n% anywhere, including, for instance, the MATLAB editor!),\r\n% and capture directly to the system clipboard!\r\n\r\n%%\r\n% As always, <https:\/\/blogs.mathworks.com\/pick\/?p=3771#respond comments to this blog post> are welcome. Or leave a\r\n% comment for Yair\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24323-screencapture#comments here>.\r\n\r\n##### SOURCE END ##### fb9f84a64ed241129c757b4b5608fe86\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Brett's Pick this week is the ScreenCapture, by Yair Altman.\r\n   \r\n   Those of you who follow the happenings on MATLAB Central (in general) are likely already familiar with Yair's... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/08\/10\/grab-a-screen-shot-from-within-matlab\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3771"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=3771"}],"version-history":[{"count":13,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3771\/revisions"}],"predecessor-version":[{"id":16377,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3771\/revisions\/16377"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}