{"id":884,"date":"2017-03-10T12:12:01","date_gmt":"2017-03-10T17:12:01","guid":{"rendered":"https:\/\/blogs.mathworks.com\/developer\/?p=884"},"modified":"2017-03-10T12:13:18","modified_gmt":"2017-03-10T17:13:18","slug":"apps-in-toolboxes","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/developer\/2017\/03\/10\/apps-in-toolboxes\/","title":{"rendered":"What About the Apps, I Hear You Ask?"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p><i>Hi all, if you are not yet aware, the R2017a release of MATLAB is now available, and it's packed full of features! If you have a chance be sure to <a href=\"https:\/\/www.mathworks.com\/products\/matlab\/whatsnew.html\">check some of them out<\/a>. Readers of this blog may be particularly interested in the <b>Advanced Software Development<\/b> section. We'll definitely be exploring more of what's new in the release soon on the blog, so stay tuned.<\/i><\/p><p><i>Today, though, I am excited to have Amy here again with more goodness on creating and sharing your MATLAB toolboxes, This time, she is focusing on integrating MATLAB Apps into the toolbox. Here's Amy.<\/i><\/p><p>In a <a href=\"https:\/\/blogs.mathworks.com\/developer\/2017\/01\/13\/matlab-toolbox-best-practices\/\">previous post<\/a>, I described how to set up your development environment when developing and packaging a MATLAB toolbox. As a result of that discussion, <a href=\"https:\/\/blogs.mathworks.com\/developer\/2017\/01\/13\/matlab-toolbox-best-practices\/#comment-9770\">Curtis asked<\/a> about packaging MATLAB Apps in toolboxes using this workflow, and I'll be talking about some of the best practices in this post.<\/p><p><a href=\"https:\/\/uk.mathworks.com\/help\/matlab\/creating_guis\/apps-overview.html\">MATLAB Apps<\/a> are interactive graphical applications written to perform technical computing tasks and can be launched from the Apps tab of the MATLAB Toolstrip. They were introduced in R2012b before custom toolboxes. With the introduction of custom toolboxes, it's natural to want to include apps within toolboxes as apps can provide an easy access point to code contained within your toolbox.<\/p><p>The toolbox packaging tool recognizes any <tt>.mlappinstall<\/tt> files within the specified toolbox folder and includes the apps in the <tt>.mltbx<\/tt> file automatically as shown in the screenshot below.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/y2017ToolboxPackagingApps_01.png\" alt=\"\"> <\/p><!--\/introduction--><p><b>Why package an app within a toolbox?<\/b><\/p><p>Toolbox code is available for use by end users either at the MATLAB command line or in user scripts once the toolbox is installed. By contrast, an app can be launched just by clicking on the icon in the Apps tab of the MATLAB Toolstrip. MATLAB will automaticaly install apps packaged with your toolbox and adds them to the Apps Gallery. You might want to have:<\/p><div><ul><li>an app that demonstrates how to use functions in your toolbox;<\/li><li>more than one app which shares the same toolbox code;<\/li><li>apps that launch other apps in the same toolbox;<\/li><li>commands that can launch apps from the command line or in user scripts after the toolbox is installed.<\/li><\/ul><\/div><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/y2017ToolboxPackagingApps_02.png\" alt=\"\"> <\/p><p><b>Where do you package the app code?<\/b><\/p><p>Like the toolbox packaging tool, the app packaging tool performs code dependency analysis to identify files required to run an app and includes these files in the package automatically. If an app is packaged within a toolbox, it is important that these files are not duplicated or packaged more than once in the <tt>.mltbx<\/tt> file. A MATLAB App requires a main or app launcher file, which typically is a simple function that launches the UI. To avoid duplication, I suggest only packaging the app launcher file in the <tt>.mlappinstall<\/tt> file, and all other code and dependencies are packaged in the <tt>.mltbx<\/tt> file. In other words, when packaging the app, remove all app files apart from the app launcher file from the MATLAB path so that these files aren't identified in the dependency analysis.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/developer\/files\/y2017ToolboxPackagingApps_03.png\" alt=\"\"> <\/p><p><b>App launcher file<\/b><\/p><p>Here is a simple app launcher file:<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">function<\/span> varargout = myAppLauncher()\r\n<span class=\"comment\">%myAppLauncher  wrapper to launch 'myApp'<\/span>\r\n<span class=\"comment\">%<\/span>\r\n<span class=\"comment\">%   f = myAppLauncher() launches 'myApp' and returns<\/span>\r\n<span class=\"comment\">%   the handle to the figure window.<\/span>\r\n\r\n<span class=\"comment\">% Create application<\/span>\r\nfh = myApp();\r\n\r\n<span class=\"comment\">% Return figure handle<\/span>\r\n<span class=\"keyword\">if<\/span> nargout == 1\r\n   varargout{1} = fh;\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<span class=\"keyword\">end<\/span> <span class=\"comment\">% myAppLauncher<\/span>\r\n\r\n<\/pre><p><b>Try it!<\/b><\/p><p>Give it a try and let us know what you think by leaving a comment below!<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_a463435577dd40ce9b74d546975ed9e4() {\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='a463435577dd40ce9b74d546975ed9e4 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a463435577dd40ce9b74d546975ed9e4';\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 2017 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_a463435577dd40ce9b74d546975ed9e4()\"><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; R2017a<br><\/p><\/div><!--\r\na463435577dd40ce9b74d546975ed9e4 ##### SOURCE BEGIN #####\r\n%% \r\n%\r\n% _Hi all, if you are not yet aware, the R2017a release of MATLAB is now\r\n% available, and it's packed full of features! If you have a chance be sure\r\n% to <https:\/\/www.mathworks.com\/products\/matlab\/whatsnew.html check some of\r\n% them out>. Readers of this blog may be particularly interested in the\r\n% *Advanced Software Development* section. We'll definitely be exploring\r\n% more of what's new in the release soon on the blog, so stay tuned._\r\n%\r\n% _Today, though, I am excited to have Amy here again with more goodness on\r\n% creating and sharing your MATLAB toolboxes, This time, she is focusing on\r\n% integrating MATLAB Apps into the toolbox. Here's Amy._\r\n% \r\n% In a\r\n% <https:\/\/blogs.mathworks.com\/developer\/2017\/01\/13\/matlab-toolbox-best-practices\/\r\n% previous post>, I described how to set up your development environment\r\n% when developing and packaging a MATLAB toolbox. As a result of that discussion,\r\n% <https:\/\/blogs.mathworks.com\/developer\/2017\/01\/13\/matlab-toolbox-best-practices\/#comment-9770\r\n% Curtis asked> about packaging MATLAB Apps in toolboxes using this\r\n% workflow, and I'll be talking about some of the best practices in this\r\n% post.\r\n%\r\n% <https:\/\/uk.mathworks.com\/help\/matlab\/creating_guis\/apps-overview.html\r\n% MATLAB Apps> are interactive graphical applications written to perform\r\n% technical computing tasks and can be launched from the Apps tab of the\r\n% MATLAB Toolstrip. They were introduced in R2012b before custom toolboxes.\r\n% With the introduction of custom toolboxes, it's natural to want to\r\n% include apps within toolboxes as apps can provide an easy access point to\r\n% code contained within your toolbox.\r\n%\r\n% The toolbox packaging tool recognizes any |.mlappinstall| files within\r\n% the specified toolbox folder and includes the apps in the |.mltbx| file\r\n% automatically as shown in the screenshot below.\r\n% \r\n% <<y2017ToolboxPackagingApps_01.png>>\r\n% \r\n%\r\n%%\r\n% *Why package an app within a toolbox?*\r\n%\r\n% Toolbox code is available for use by end users either at the MATLAB\r\n% command line or in user scripts once the toolbox is installed. By\r\n% contrast, an app can be launched just by clicking on the icon in the Apps\r\n% tab of the MATLAB Toolstrip. MATLAB will automaticaly install apps\r\n% packaged with your toolbox and adds them to the Apps Gallery. You might\r\n% want to have:\r\n%%\r\n% * an app that demonstrates how to use functions in your toolbox;\r\n% * more than one app which shares the same toolbox code;\r\n% * apps that launch other apps in the same toolbox;\r\n% * commands that can launch apps from the command line or in user scripts\r\n% after the toolbox is installed.\r\n% \r\n% <<y2017ToolboxPackagingApps_02.png>>\r\n% \r\n%\r\n%\r\n%% \r\n% *Where do you package the app code?*\r\n%\r\n% Like the toolbox packaging tool, the app packaging tool performs code\r\n% dependency analysis to identify files required to run an app and includes\r\n% these files in the package automatically. If an app is packaged within a\r\n% toolbox, it is important that these files are not duplicated or packaged\r\n% more than once in the |.mltbx| file. A MATLAB App requires a main or app\r\n% launcher file, which typically is a simple function that launches the UI.\r\n% To avoid duplication, I suggest only packaging the app launcher file in\r\n% the |.mlappinstall| file, and all other code and dependencies are\r\n% packaged in the |.mltbx| file. In other words, when packaging the app,\r\n% remove all app files apart from the app launcher file from the MATLAB\r\n% path so that these files aren't identified in the dependency analysis.\r\n% \r\n% <<y2017ToolboxPackagingApps_03.png>>\r\n% \r\n%\r\n%\r\n%%\r\n% *App launcher file*\r\n%\r\n% Here is a simple app launcher file: \r\n%%\r\n% \r\n% <include>myAppLauncher.m<\/include>\r\n% \r\n%\r\n%%\r\n% *Try it!*\r\n%\r\n% Give it a try and let us know what you think by leaving a comment below!\r\n\r\n##### SOURCE END ##### a463435577dd40ce9b74d546975ed9e4\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/developer\/files\/y2017ToolboxPackagingApps_03.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><!--introduction--><p><i>Hi all, if you are not yet aware, the R2017a release of MATLAB is now available, and it's packed full of features! If you have a chance be sure to <a href=\"https:\/\/www.mathworks.com\/products\/matlab\/whatsnew.html\">check some of them out<\/a>. Readers of this blog may be particularly interested in the <b>Advanced Software Development<\/b> section. We'll definitely be exploring more of what's new in the release soon on the blog, so stay tuned.<\/i>... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/developer\/2017\/03\/10\/apps-in-toolboxes\/\">read more >><\/a><\/p>","protected":false},"author":90,"featured_media":888,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/884"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/comments?post=884"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/884\/revisions"}],"predecessor-version":[{"id":893,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/884\/revisions\/893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/media\/888"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/media?parent=884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/categories?post=884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/tags?post=884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}