Main Content


Execute the specified MATLAB script in data source workspace

Since R2022b



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

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


    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.


    Also check the value of variable 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.

    getVariables(sourceWorkspace,["p" "x"])
    ans =
    1x2 cell array
        {[3]}  {[10]}

    Input Arguments

    collapse all

    Data source workspace, specified as a 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