Guy on Simulink

Simulink & Model-Based Design

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.

|

Comments

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