{"id":11561,"date":"2020-06-09T19:27:14","date_gmt":"2020-06-09T23:27:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=11561"},"modified":"2020-06-09T19:27:14","modified_gmt":"2020-06-09T23:27:14","slug":"copying-uiaxes-graphics","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2020\/06\/09\/copying-uiaxes-graphics\/","title":{"rendered":"Copying uiaxes graphics"},"content":{"rendered":"\r\n<div class=\"content\"><p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/73103\"><tt>copyUIAxes<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3753776\">Adam Danz<\/a>.<\/p><p>Many of you may know Adam from both File Exchange and MATLAB Answers. On File Exchange, he is an author of many high-quality entries, with 3 Pick of the Week selections.<\/p><p><tt>copyUIAxes<\/tt> is one of those functions that has a clear need but is quite daunting to tackle because of the complexity. The idea started from this thread on MATLAB Answers, \" <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/281318-how-can-i-save-a-figure-within-app-designer\">How can I save a figure within app designer?<\/a>\" You can see that there was quite an active discussion around the quesiton and solutions. It turns out that the function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/copyobj.html\"><tt>copyobj<\/tt><\/a>, which lets you copy graphics objects from one parent handle to another, does not work with <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/uiaxes.html\"><tt>uiaxes<\/tt><\/a>. <tt>uiaxes<\/tt> is a (relatively) new type of graphics object that came about with the introduction of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/app-designer.html\">App Designer<\/a>. It is different from the traditional <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/axes.html\"><tt>axes<\/tt><\/a>, and some of the commands that work with the legacy graphics system do not work with the new graphics objects. <tt>copyobj<\/tt> is one of those commands.<\/p><p>Copying objects from a <tt>uiaxes<\/tt> allows you to display the content on a traditional <tt>axes<\/tt>, and that lets you easily save, print, or modify the graph. The challenge is that many axes properties are \"hidden, obscured, read-only, or vary between Matlab releases\", as Adam mentions in the description. If you look at his code, he has lots of validation, error-checking, special cases, and other techniques to be as general and multi-release compatible as possible.<\/p><p>I also encourage you to take a look inside the code, as Adam includes some details about the code as well as the complexity of the task.<\/p><p>Adam mentions this in the File Exchange entry page, but in R2020a, there are two new functions <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/copygraphics.html\"><tt>copygraphics<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/exportgraphics.html\"><tt>exportgraphics<\/tt><\/a> that let you copy or save your graphs created in <tt>uiaxes<\/tt>. But nonetheless, his <tt>copyUIAxes<\/tt> is valuable for people using older releases.<\/p><p>\r\n<img\r\nsrc=\"https:\/\/jp.mathworks.com\/matlabcentral\/mlc-downloads\/downloads\/64c2c6ba-a56d-4750-8771-5a72d2d8acb4\/4793d8d6-bb38-4e86-8d9d-e46fb6943905\/images\/screenshot.png\"\r\nwidth=900>\r\n<\/p><p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=11561#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/73103#comment\">comment<\/a> for Adam.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_4a862861b9fb46bebed1b715e96be14d() {\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='4a862861b9fb46bebed1b715e96be14d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 4a862861b9fb46bebed1b715e96be14d';\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        copyright = 'Copyright 2020 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_4a862861b9fb46bebed1b715e96be14d()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2020a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2020a<br><\/p><\/div><!--\r\n4a862861b9fb46bebed1b715e96be14d ##### SOURCE BEGIN #####\r\n%%\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/73103 |copyUIAxes|>\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3753776 Adam\r\n% Danz>.\r\n%\r\n% Many of you may know Adam from both File Exchange and MATLAB Answers. On\r\n% File Exchange, he is an author of many high-quality entries, with 3 Pick\r\n% of the Week selections.\r\n%\r\n% |copyUIAxes| is one of those functions that has a clear need but is quite\r\n% daunting to tackle because of the complexity. The idea started from this\r\n% thread on MATLAB Answers, \"\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/281318-how-can-i-save-a-figure-within-app-designer\r\n% How can I save a figure within app designer?>\" You can see that there was\r\n% quite an active discussion around the quesiton and solutions. It turns\r\n% out that the function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/copyobj.html |copyobj|>, which\r\n% lets you copy graphics objects from one parent handle to another, does\r\n% not work with <https:\/\/www.mathworks.com\/help\/matlab\/ref\/uiaxes.html\r\n% |uiaxes|>. |uiaxes| is a (relatively) new type of graphics object that came\r\n% about with the introduction of\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/app-designer.html App Designer>.\r\n% It is different from the traditional\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/axes.html |axes|>, and some of\r\n% the commands that work with the legacy graphics system do not work with\r\n% the new graphics objects. |copyobj| is one of those commands.\r\n%\r\n% Copying objects from a |uiaxes| allows you to display the content on a\r\n% traditional |axes|, and that lets you easily save, print, or modify the\r\n% graph. The challenge is that many axes properties are \"hidden, obscured,\r\n% read-only, or vary between Matlab releases\", as Adam mentions in the\r\n% description. If you look at his code, he has lots of validation,\r\n% error-checking, special cases, and other techniques to be as general and\r\n% multi-release compatible as possible.\r\n%\r\n% I also encourage you to take a look inside the code, as Adam includes\r\n% some details about the code as well as the complexity of the task.\r\n%\r\n% Adam mentions this in the File Exchange entry page, but in R2020a, there\r\n% are two new functions\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/copygraphics.html\r\n% |copygraphics|> and\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/exportgraphics.html\r\n% |exportgraphics|> that let you copy or save your graphs created in\r\n% |uiaxes|. But nonetheless, his |copyUIAxes| is valuable for people using\r\n% older releases.\r\n%\r\n% <html>\r\n% <img\r\n% src=\"https:\/\/jp.mathworks.com\/matlabcentral\/mlc-downloads\/downloads\/64c2c6ba-a56d-4750-8771-5a72d2d8acb4\/4793d8d6-bb38-4e86-8d9d-e46fb6943905\/images\/screenshot.png\"\r\n% width=900>\r\n% <\/html>\r\n%\r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <http:\/\/blogs.mathworks.com\/pick\/?p=11561#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/73103#comment comment> for Adam.\r\n\r\n##### SOURCE END ##### 4a862861b9fb46bebed1b715e96be14d\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/pick\/files\/copyuiaxes_screenshot.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>\r\nJiro's Pick this week is copyUIAxes by Adam Danz.Many of you may know Adam from both File Exchange and MATLAB Answers. On File Exchange, he is an author of many high-quality entries, with 3 Pick of... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2020\/06\/09\/copying-uiaxes-graphics\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":11565,"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\/11561"}],"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\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=11561"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11561\/revisions"}],"predecessor-version":[{"id":11571,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11561\/revisions\/11571"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media\/11565"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=11561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=11561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=11561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}