Main Content

run

Execute the specified MATLAB script in data source workspace

Since R2022b

    Description

    run(dsWks,mscript) executes the specified MATLAB® script in the data source workspace represented by the Simulink.data.DataSourceWorkspace object dsWks.

    While executing a script on the data source workspace, MATLAB temporarily changes the current folder to the folder that contains the script.

    example

    Examples

    collapse all

    Write a short script that sets some variables in a data source workspace.

    % myscript.m
    x = 10;
    p = p+1;

    Before running the script, check the current value of variable p in the data source workspace object sourceWorkspace.

    getVariable(sourceWorkspace,'p')
    2

    Also check the value of variable x.

    getVariable(sourceWorkspace,'x')
    Error: undefined variable 'x'

    The output shows that x is not yet defined in the workspace.

    Now run the script myscript.m and check the values of p and x again.

    run(sourceWorkspace,'myscript.m');
    getVariables(sourceWorkspace,["p" "x"])
    ans =
    
    1x2 cell array
    
        {[3]}  {[10]}

    Input Arguments

    collapse all

    Data source workspace, specified as a Simulink.data.DataSourceWorkspace object.

    MATLAB script, specified as a character vector or string scalar. The script must be a .m or .mlx file on the MATLAB path.

    Example: 'mfun.m'

    Version History

    Introduced in R2022b