Guy on Simulink

Simulink & Model-Based Design

Organizing Mask Callbacks

My recent posts have focused on the different elements required to build a library block.  There is the conceptual design of the block, the dynamic dialog, and the mask initialization code.  In each of these posts, I have attempted to focus on just the core of those concepts without complicating things and covering too much.  However, I was not showing you my actual code.  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.

M-code switchyards

In the Saturation block example, M-code is used for mask initialization, and dialog callbacks.  To keep the callbacks organized I put them into a switchyard style M-file function that contains all the code for the block.  The calling syntax passes the inputs to the appropriate local function. Here is the primary function in saturation_cb.m
.

function saturation_cb(blk,callback,uplimsrc,uplim,lowlimsrc,lowlim)
% implements the callbacks for saturation block
switch callback
    case 'init'
        init_cb(blk,uplimsrc,uplim,lowlimsrc,lowlim)
    case 'uplimsrc'
        uplimsrc_cb(blk)
    case 'lowlimsrc'
        lowlimsrc_cb(blk)
    otherwise
        disp('saturation_cb: callback not implemented')
end
 

The mask callbacks run the primary function with the same first two inputs, the block path, blk, and the name for the callback. The dialog callback for the uplimsrc parameter calls this function, saturation_cb.m

Mask dialog callback

The lowlimsrc dialog callback is similar.  Execution flows through the primary function to the local function uplimsrc_cb.

This local function is where the real work happens.  The primary function just dispatches to the appropriate local function.  In the case of the mask initialization code, we have access to all the mask variables in the workspace.  For this reason, I pass these in as additional inputs.

Mask Initialization Callback

This call passes through the switchyard to call the init_cb local function.

I like working in the MATLAB editor

My friends on the Desktop development team have spoiled me.  I prefer the debugging tools that I get from working in the MATLAB editor.  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.

Now it’s your turn

This is how I keep mask code organized.  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?  Leave me a comment here.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.