{"id":28,"date":"2008-08-28T23:00:19","date_gmt":"2008-08-29T04:00:19","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2008\/08\/28\/organizing-mask-callbacks\/"},"modified":"2015-07-29T08:41:18","modified_gmt":"2015-07-29T13:41:18","slug":"organizing-mask-callbacks","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2008\/08\/28\/organizing-mask-callbacks\/","title":{"rendered":"Organizing Mask Callbacks"},"content":{"rendered":"<p>My recent posts have focused on the different elements required to build a library block.\u00a0 There is the <a href=\"https:\/\/blogs.mathworks.com\/seth\/2008\/08\/05\/advanced-masking-concepts\/\">conceptual design of the block<\/a>, the <a href=\"https:\/\/blogs.mathworks.com\/seth\/2008\/08\/13\/dynamic-mask-dialogs\/\">dynamic dialog<\/a>, and the <a href=\"https:\/\/blogs.mathworks.com\/seth\/2008\/08\/21\/mask-initialization-and-self-modifying-blocks\/\">mask initialization code<\/a>.\u00a0 In each of these posts, I have attempted to focus on\r\njust the core of those concepts without complicating things and covering too much.\u00a0 However, I was not showing you <em>my actual code<\/em>.\u00a0 Now, I want to step back and show you the big picture view of how I organize the code for my masked blocks in Simulink.<\/p>\r\n\r\n<p><strong>M-code switchyards<\/strong><\/p>\r\n\r\n<p>In the Saturation block example, M-code is used for mask initialization, and dialog callbacks.\u00a0 To keep the callbacks organized I put them into a <a href=\"https:\/\/web.archive.org\/web\/20050217234949\/http:\/www.mathworks.com\/company\/newsletters\/news_notes\/win00\/prog_patterns.html\">switchyard style<\/a> M-file function that contains all the code for the block.\u00a0 The calling syntax passes the inputs to the appropriate <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/local-functions.html\"> local function<\/a>. Here is the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/create-functions-in-files.html\">primary function<\/a> in <a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/saturation_cb.m\">saturation_cb.m<\/a><br \/>.<\/p>\r\n\r\n<div class=\"content\">\r\n<pre class=\"codeinput\"><span class=\"keyword\">function<\/span> saturation_cb(blk,callback,uplimsrc,uplim,lowlimsrc,lowlim)\r\n<span class=\"comment\">% implements the callbacks for saturation block<\/span>\r\n<span class=\"keyword\">switch<\/span> callback\r\n    <span class=\"keyword\">case<\/span> <span class=\"string\">'init'<\/span>\r\n        init_cb(blk,uplimsrc,uplim,lowlimsrc,lowlim)\r\n    <span class=\"keyword\">case<\/span> <span class=\"string\">'uplimsrc'<\/span>\r\n        uplimsrc_cb(blk)\r\n    <span class=\"keyword\">case<\/span> <span class=\"string\">'lowlimsrc'<\/span>\r\n        lowlimsrc_cb(blk)\r\n    <span class=\"keyword\">otherwise<\/span>\r\n        disp(<span class=\"string\">'saturation_cb: callback not implemented'<\/span>)\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre>\r\n<\/div>\r\n&nbsp;\r\n\r\n<p>The mask callbacks run the primary function with the same first two inputs, the block path, <em>blk<\/em>, and the name for the <em>callback<\/em>. The dialog callback for the uplimsrc parameter calls this function, saturation_cb.m <br \/><br \/><\/p>\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/uplimsrc_cb.png\" alt=\"Mask dialog callback\" \/> <br \/>\r\n\r\n<p>The lowlimsrc dialog callback is similar.\u00a0 Execution flows through the primary function to the local function uplimsrc_cb.<\/p>\r\n\r\n<p>This local function is where the real work happens.\u00a0 The primary function just dispatches to the appropriate local function.\u00a0 In the case of the mask initialization code, we have access to all the mask variables in the workspace.\u00a0 For this reason, I pass these in as additional inputs. <br \/><br \/><\/p>\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/init_cb.png\" alt=\"Mask Initialization Callback\" \/><br \/>\r\n\r\n<p>This call passes through the switchyard to call the init_cb local function.<\/p>\r\n\r\n<p><strong>I like working in the MATLAB editor<\/strong><\/p>\r\n\r\n<p>My friends on the Desktop development team have spoiled me.\u00a0 I prefer the debugging tools that I get from working in the MATLAB editor.\u00a0 The switchyard callback M-file makes it easier to edit and debug as I develop the block. I find that the space in the mask editor is only comfortable to work in when the callback is a few lines at most.<\/p>\r\n\r\n<p><strong>Now it\u2019s your turn<\/strong><\/p>\r\n\r\n<p>This is how I keep mask code organized.\u00a0 I use this basic system every time I develop a new block. How do you keep your code organized? Do you prefer another style of switchyard function?\u00a0 Leave me a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=28&amp;#comment\">comment here<\/a>.<br \/><br \/><\/p>","protected":false},"excerpt":{"rendered":"<p>My recent posts have focused on the different elements required to build a library block.\u00a0 There is the conceptual design of the block, the dynamic dialog, and the mask initialization code.\u00a0 In each... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2008\/08\/28\/organizing-mask-callbacks\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[47],"tags":[48,441],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/28"}],"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\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":4710,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/28\/revisions\/4710"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}