{"id":2330,"date":"2013-06-24T03:11:47","date_gmt":"2013-06-24T08:11:47","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=2330"},"modified":"2013-06-24T08:54:20","modified_gmt":"2013-06-24T13:54:20","slug":"defining-custom-shortcuts-in-simulink","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2013\/06\/24\/defining-custom-shortcuts-in-simulink\/","title":{"rendered":"Defining custom shortcuts in Simulink"},"content":{"rendered":"<p>On this blog, I sometimes highlight features that it took me a while to appreciate and include in my daily workflow.<\/p>\r\n\r\n<p>I hate to admit it, but quite often those are features that I was aware of for a long time, but for various reasons I was scared of using. Most of the time, I had seen those used in some complex application and I thought it was very complicated to use.<\/p>\r\n\r\n<p>This week I will share something that turns out to be simpler then it looks: Adding menu items in Simulink.<\/p>\r\n\r\n<p><strong>Which task do you often need to do?<\/strong><\/p>\r\n\r\n<p>In Simulink, we try to add menu items and define shortcuts for common tasks that we know most users need to do. For example, everybody needs to save models, so we have a button on the toolbar and a shortcut (Ctrl+S).<\/p>\r\n\r\n<p>However, if there is a task you do frequently, adding custom menus and shortcuts can significantly speedup your workflow.<\/p>\r\n\r\n<p><strong>My need: Debugging Buses<\/strong><\/p>\r\n\r\n<p>In my case, I often need to debug large models made of buses, simplifying the problem by removing parts of the model. I reduce the complexity by replacing many blocks with one Constant block outputting a bus signal.\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/cuttingBuses.png\" alt=\"Replacing a bus by a Constant Block\" \/><\/p>\r\n\r\nTo do this I create bus objects and initialize the bus with a structure. One morning I got tired of typing <tt><a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/simulink.bus.createobject.html\">Simulink.Bus.createObject(bdroot,gcb)<\/a><\/tt> and <tt><a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/simulink.bus.creatematlabstruct.html\">Simulink.Bus.createMATLABStruct<\/a><\/tt>, so I decided that I wanted a shortcut.<\/p>\r\n\r\n<p><strong>Creating the sl_custmization.m file<\/strong><\/p>\r\n\r\n<p>When I realized I could do that with less than 10 lines of code, I was pretty happy:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/my_sl_customization.png\" alt=\"A simple example of customized menu\" \/><\/p>\r\n\r\n<p>Here is how it works:<\/p>\r\n\r\n<ul>\r\n\t<li>In the first function, I specify that my menu entry will be located in the Tools menu, and the menu content will be described by the function <tt>getMyMenuItems<\/tt>.<\/li>\r\n\t<li>In <tt>getMyMenuItems<\/tt>, I output a cell array of functions defining the menu entries. Multiple elements in the cell array can become sub-menus.<\/li>\r\n\t<li>In the third function <tt>createBO<\/tt>, I specify the properties of my entry. It will read <strong>Create Bus Object<\/strong>, will have the shortcut <strong>Ctrl+Shift+B<\/strong> and when triggered will execute the callback function <tt>createBOcb<\/tt>.<\/li>\r\n\t<li>The last function <tt>createBOcb<\/tt> does the work, in this case creating a bus object and a MATLAB structure based on the currently selected block (using <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/gcb.html\">gcb<\/a>).<\/li>\r\n<\/ul>\r\n\r\n<p>I could probably make the callback more robust, but I wanted to keep this example as simple as possible to highlight the concept.<\/p>\r\n\r\n<p><strong>The Final Result<\/strong><\/p>\r\n\r\n<p>Once this file is saved somewhere on my path, I refresh the customizations using <tt>sl_refresh_customization<\/tt> and I can use my new entry from the Tools menu, or using Ctrl+Shift+B.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/customized_menu.png\" alt=\"Customized menu\" \/><\/p>\r\n\r\n<p><strong>My other customizations<\/strong><\/p>\r\n\r\n<p>Here are a few other things for which I created shortcuts using sl_customization files<\/p>\r\n\r\n<ul>\r\n\t<li>Launching the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28184-simscape-simulation-results-explorer\">Simscape Simulation Results Explorer<\/a><\/li>\r\n\t<li>Executing <a href=\"https:\/\/blogs.mathworks.com\/seth\/2012\/06\/04\/the-most-useful-command-for-debugging-variable-step-solver-performance\/\">THE Most Useful Command for Debugging Variable Step Solver Performance<\/a>\r\n<\/ul>\r\n\r\n\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>For more examples, I recommend looking at the MATLAB Central submission highlighted by Seth in a <a href=\"https:\/\/blogs.mathworks.com\/seth\/2009\/04\/29\/pick-of-the-week-customizing-simulink\/\">previous post<\/a>: <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23943-customizing-the-simulink-interface-demo\">Customizing the Simulink Interface Demo<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/30904\">Saurabh Mahapatra<\/a>.<\/p>\r\n\r\n<p>Do you add menu entries and shortcuts for the tasks you accomplish frequently? What are those tasks? Let us know by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=2330&#comment\">comment here<\/a>.<\/p>\r\n\r\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q2\/customized_menu.png\" onError=\"this.style.display ='none';\" \/><\/div><p>On this blog, I sometimes highlight features that it took me a while to appreciate and include in my daily workflow.\r\n\r\nI hate to admit it, but quite often those are features that I was aware of for... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/06\/24\/defining-custom-shortcuts-in-simulink\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,33],"tags":[327],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2330"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=2330"}],"version-history":[{"count":35,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2330\/revisions"}],"predecessor-version":[{"id":2420,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2330\/revisions\/2420"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=2330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=2330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=2330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}