Guy and Seth on Simulink

April 22nd, 2010

Variables You Depend On

Have you ever sent someone a Simulink model only to find out they couldn't run it because you didn't include the required variables? MathWorks technical support has this problem on a daily basis. This week guest blogger Parasar Kodati shares with us a new feature to address this issue called Simulink.findVars.

Simulink variable finder, Parasar Kodati

A New Feature for Variable Management

We all use variables in our models. Variables help us identify the parameters that determine the behavior of the model and vary them as needed. I am sure you would agree that it is very easy to introduce variables in a model but difficult to find and keep track of them. One common issue I have seen as a Tech-Support engineer is variables defined with the same name at multiple places in a model. It is also hard to know where in a model a variable is. The Simulink.findVars command makes it very easy to identify where a model uses a given variable and in which workspace it is stored. Check out the video I recorded to demonstrate this feature. In this post, I will show you how to use the Simulink.findVars command with the well-known demo model, sf_car:

find_vehicledata = Simulink.findVars('sf_car','Name','vehicledata')
find_vehicledata = 

  Simulink.WorkspaceVar handle
  Package: Simulink

  Properties:
             Name: 'vehicledata'
        Workspace: 'sf_car'
    WorkspaceType: 'model'
     UsedByBlocks: {'sf_car/Vehicle'}

The Properties section of the above output shows that the model uses the variable vehicledata, it is used by the block sf_car/Vehicle, and stored in the model workspace (see the WorkspaceType property). This is why if you try to change the variable vehicledata in the base workspace, it has no effect. Also, note that the return object find_vehicledata is of type Simulink.WorkspaceVar.

Simulink uses variables from the base, model and mask workspace, Simulink.findVars can identify where a variable is from.

Another scenario is when you have many variables in your MATLAB base workspace and you want to know which ones you need to simulate a model. Instead of using the Name property this time, you can use the WorkspaceType property as follows:

 base_vars = Simulink.findVars('sf_car','WorkspaceType','base')
base_vars = 

  0x0 empty Simulink.WorkspaceVar handle
  Package: Simulink

  Properties:
    Name
    Workspace
    WorkspaceType
    UsedByBlocks

This time the return object is empty indicating that the model is not using any of the base workspace variables. I hope that Simulink.findVars provides you a way to manage your workspaces and variables.

Now it's your turn

The Simulink.findVars documentation includes more options and use-cases. Try out Simulink.findVars on your models and let us know what you find with a comment here.

6 Responses to “Variables You Depend On”

  1. wei replied on :

    @Parasar, Nice feature.
    I don’t have 10a and have only read doc. Would you give some detail on mask workspace? How is findVars return relate to MaskVariables? What happen to MaskInitialization (which can fail) created variables, esp. with cached find?

    Workspace is important subject. Please explain cases for nested maskspace, linked library, and reference model.

  2. Parasar Kodati replied on :

    @Wei

    Glad you liked the feature. You can apply the Simulink.findVars for Mask workspaces too. Variables declared under Mask Initialization can be found as long as they are used as block parameters for any of the blocks inside the masked subsystem.

    Nested Masks: I am not sure what you would like to do with mask workspaces that are nested. Please note that once you get the Simulink.WorkspaceVar objects (here for the mask workspaces) you can use methods like intersect and setdiff to compare the workspaces.

    Reference Model: Simulink.findVars can find base workspace variables used that are used in child models. However Simulink.findVars only sees the model workspace of the parent model. You can obtain the model workspaces of individual child reference models by specifying the model name separately.

    Hope this helps
    Parasar

  3. Naresh replied on :

    The Simulink.findVars is not working for me
    matlab returns an error
    ??? Undefined variable “Simulink” or class “Simulink.findVars”.

    i tried this in the matlab command window and got this error and am using R2007b

  4. Guy replied on :

    @Naresh, Simulink.findVars is a new feature introduced in MATLAB R2010a. With a license under maintenance, you can update your installation in the suppoert section of the website.

  5. Mousa Marzband replied on :

    There are some of variable in the SIMULINK demo which I don’t know their values. Could you please explain me how I can find their initial values?

  6. Seth Popinchalk replied on :

    @Mousa Marzband – If you look at the workspace returned in the Simulink.WorkspaceVar object, you can determine where the variable is. Then you can evaluate it. This is explained in the documentation for Simulink.WorkspaceVar.

    For example, if the variable ‘x’ is in the ‘base’ workspace, you can call:

    >> x
    
    or 
    
    >> evalin('base','x')
    

    If the variabel is in the model workspace, I reccomend looking into the model explorer. This way you can see the variable in the model workspace. If you need to do it programatically, the documentation on Using MATLAB Commands to Change Workspace Data illustrates how to do this.

    Finally, there are some variables burried in blocks in the mode and it takes a little work to see them. The Mask workspace isn’t accessible through those methods. You can use a get_param command to see the MaskWSVariables:

    get_param(s(1).Workspace,'MaskWSVariables')
    

    I hope this helps!

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks
Guy Rouleau and Seth Popinchalk are Application Engineers for MathWorks. They write here about Simulink and other MathWorks tools used in Model-Based Design.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.