Main Content

bind

Connect app components to simulation signals and variables

Since R2024a

    Description

    example

    b = bind(sigs,sigpath,scope) connects a logged signal in a Simulink® model to a time scope UI component in an app. When the signal logs data, the time scope displays the data in the app. The function returns the Binding object b, which represents the connection.

    example

    b = bind(sigs,sigpath,c,cprop) connects a logged signal in a Simulink model to a property of a UI component in an app. When the signal logs data, the component property value updates with the new data.

    example

    b = bind(c,cprop,vars,varname) connects a property of a UI component to a model variable. When the component property changes, the model variable updates with the new property value.

    example

    b = bind(___,Enabled=tf) specifies the operational state of the binding. You can modify the operational state of the binding after creating it by setting the Enabled property of the Binding object b.

    Examples

    collapse all

    Visualize signal data in an app while a simulation runs by connecting a logged signal in a Simulink model to a time scope in an app.

    First, create a time scope component in a UI figure.

    fig = uifigure;
    scope = uitimescope(fig);

    Create a Simulation object that represents a simulation of the bouncingBall model and load the model.

    s = simulation('bouncingBall',LoadModel=true);

    Connect a logged signal in the model to the time scope component by creating a binding. Specify the binding source by using the collection of signals logged in the Simulation object along with the block path and port index of the specific signal to visualize. Specify the binding destination as the TimeScope object.

    sig = s.LoggedSignals;
    tindoors = s.ModelName + "/Second-Order  Integrator:1";
    bind(sig,tindoors,scope);

    Start the simulation. The time scope displays the signal data as the simulation runs.

    start(s)

    Time scope UI component with some plotted signal data

    View the value of a signal in an app while a simulation runs by connecting a logged signal in a Simulink model to a gauge UI component.

    First, create a gauge in a UI figure. Specify the gauge limits to span the expected signal values.

    fig = uifigure;
    g = uigauge(fig,Limits=[0 30]);

    Create a Simulation object that represents a simulation of the bouncingBall model and load the model.

    s = simulation('bouncingBall',LoadModel=true);

    Connect a logged signal in the model to the gauge component by creating a binding. Specify the binding source by using the collection signals logged in the Simulation object along with the block path and port index of the specific signal to visualize. Specify the binding destination as the Value property of the Gauge object.

    sig = s.LoggedSignals;
    tindoors = s.ModelName + "/Second-Order  Integrator:1";
    bind(sig,tindoors,g,"Value");

    Start the simulation. The gauge displays the signal value as the simulation runs.

    start(s)

    Gauge in a UI figure window. The gauge needle is pointing to a value between 5 and 10.

    Connect a spinner UI component to a model variable to tune the variable from an app while the simulation runs.

    First, create a spinner and a time scope in a UI figure. Specify the spinner limits, value, and step size to match the expected values of the model variable.

    fig = uifigure;
    spn = uispinner(fig,Limits=[-1 0],Value=-0.8,Step=0.1);
    scope = uitimescope(fig,Position=[100 150 300 200]);

    Create a Simulation object that represents a simulation of the bouncingBall model and load the model.

    s = simulation('bouncingBall',LoadModel=true);

    Connect the spinner to a model variable by creating a binding. Specify the binding source as the Value property of the Spinner object. Specify the binding destination by using the collection of tunable variables of the Simulation object along with the specific variable name that you want to connect to.

    vars = s.TunableVariables;
    varname = "K:bouncingBall";
    bind(spn,"Value",vars,varname);

    When you change the value of the spinner, the value of the model variable updates to match. Visualize the effect of tuning the variable by connecting a signal in the model to the time scope.

    sig = s.LoggedSignals;
    tindoors = s.ModelName + "/Second-Order  Integrator:1";
    bind(sig,tindoors,scope);

    Start the simulation. Interact with the spinner to tune the variable while the simulation runs.

    start(s)

    Spinner and time scope in a UI figure window. The spinner has a value of -0.1 and the shape of the plotted signal has changed halfway along the time axis.

    Create a slider in a UI figure.

    fig = uifigure;
    sld = uislider(fig,Limits=[-1 0],Value=-0.8);

    Create a Simulation object that represents a simulation of the bouncingBall model and load the model.

    s = simulation('bouncingBall',LoadModel=true);

    Connect the slider value to a variable in the model by creating a binding. Store the Binding object in a variable, b, so you can modify the binding after creation.

    vars = s.TunableVariables;
    varname = "K:bouncingBall";
    b = bind(sld,"Value",vars,varname);

    Disable the binding by setting the Enabled property of b using dot notation.

    b.Enabled = false;

    Delete the binding using the delete function.

    delete(b)

    Input Arguments

    collapse all

    Collection of logged signals in a simulation, specified as a Signals object associated with the LoggedSignals property of a Simulation object.

    Together, sigs and sigpath specify a logged signal in a simulation to display in an app.

    Logged signal to display, specified as a string scalar or character vector. Specify sigpath using the block path and port index of the signal.

    Together, sigs and sigpath specify a logged signal in a simulation to display in an app.

    Example: "MyModel/Temperature:1"

    Time scope UI component in an app, specified as a TimeScope object.

    UI component.

    Together, c and cprop specify a component property to connect to a model element.

    Property of the UI component given by c, specified as a string scalar or character vector.

    Together, c and cprop specify a component property to connect to a model element.

    Example: "Value"

    Tunable variables in a simulation, specified as a Variables object associated with the TunableVariables property of a Simulation object.

    Together, vars and varname specify a variable in a simulation that you can tune from an app.

    Variable name, specified as a string scalar or character vector. Specify a variable in the model workspace using the variable name and model name, separated by a colon (for example, "myvar:MyModel"). Specify a variable in the global workspace using only the variable name (for example, "myvar").

    Together, vars and varname specify a variable in a simulation that you can tune from an app.

    Operational state of the binding, specified as a numeric or logical 1 (true) or 0 (false).

    Tips

    • If the data type of the source parameter is different than the data type of the destination parameter, the bind function attempts to convert the data. For example, if the source parameter is a UI component property that stores text and the destination parameter is a model variable that stores a numeric value, the bind function automatically converts the text to a numeric value.

    • You can delete a binding by calling the delete function on the returned Binding object.

    Version History

    Introduced in R2024a