Main Content

matlab.lang.Workspace

Store workspace variables

Since R2025a

    Description

    A workspace contains variables that you create in MATLAB® or import into MATLAB from data files or other programs. Use a matlab.lang.Workspace object to store a copy of the variables in a workspace. You can pass this object and access it from other workspaces in the same way as any other variable.

    Creation

    To create a matlab.lang.Workspace object, use the matlab.lang.Workspace function (described here) or one of these functions:

    • Use matlab.lang.Workspace.baseWorkspace to create a workspace object that contains a copy of variables in the base workspace. The base workspace stores variables that you create at the command line. The base workspace includes any variables that scripts create, if you run the script from the command line or from the Editor.

    • Use matlab.lang.Workspace.currentWorkspace to create a workspace object that contains a copy of variables in the current workspace. For instance, call matlab.lang.Workspace.currentWorkspace inside a function to create a workspace object that contains variables in the function workspace.

    • Use matlab.lang.Workspace.globalWorkspace to create a workspace object that contains a copy of global variables.

    Description

    w = matlab.lang.Workspace creates an empty workspace object.

    example

    w = matlab.lang.Workspace(Source=sourceWorkspace) creates a workspace object that contains a copy of variables from the specified workspace.

    example

    w = matlab.lang.Workspace(Source=sourceWorkspace,Variables=varList) creates a workspace object that contains a copy of the specified variables from the specified workspace.

    example

    Input Arguments

    expand all

    Source workspace, specified as a matlab.lang.Workspace object.

    Variables to copy, specified as a string array, character vector, or cell array of character vectors containing the names of variables to copy.

    Output Arguments

    expand all

    Workspace, returned as a matlab.lang.Workspace object.

    Object Functions

    matlab.lang.Workspace.baseWorkspaceStore variables from base workspace
    matlab.lang.Workspace.currentWorkspaceStore variables from current workspace
    matlab.lang.Workspace.globalWorkspaceStore variables from global workspace
    variablesInformation about workspace variables
    variableNamesNames of variables in workspace
    evaluateAndCaptureEvaluate MATLAB code in specified workspace

    Examples

    collapse all

    Use workspace objects to store variables from different workspaces in MATLAB.

    Create a set of variables in the base workspace.

    a = 1;
    b = true;
    c = "Hello, World";

    Use matlab.lang.Workspace.baseWorkspace to create a workspace object that stores the variables in the base workspace.

    wBase = matlab.lang.Workspace.baseWorkspace
    wBase = 
    Workspace with variables:
    
        Name    Size     Class 
        ____    ____    _______
    
         a      1x1     double 
         b      1x1     logical
         c      1x1     string 
    
    

    Create a set of variables in the global workspace.

    global x y z
    x = 2;
    y = false;
    z = "Goodnight, Moon";

    Clear the global variables.

    clear x y z

    Use matlab.lang.Workspace.globalWorkspace to create a workspace object that stores the variables in the global workspace.

    wGlobal = matlab.lang.Workspace.globalWorkspace
    wGlobal = 
    Workspace with variables:
    
        Name    Size     Class 
        ____    ____    _______
    
         x      1x1     double 
         y      1x1     logical
         z      1x1     string 
    
    

    Clear the global workspace.

    clear x y z

    Create a function that uses matlab.lang.Workspace.currentWorkspace to create a workspace object that stores the variables in the current function workspace.

    function wCurrent = exampleFunction(input)
    localVar = input + 5;
    wCurrent = matlab.lang.Workspace.currentWorkspace;
    end

    Run exampleFunction.

    wCurrent = exampleFunction(a)
    wCurrent = 
    Workspace with variables:
    
          Name      Size    Class 
        ________    ____    ______
    
        input       1x1     double
        localVar    1x1     double
    
    

    Create an empty workspace object.

    w = matlab.lang.Workspace
    w = 
    Workspace with no variables.
    
    

    Create several variables in the base workspace.

    a = 1;
    b = true;
    c = "Hello, World";

    Use matlab.lang.Workspace.baseWorkspace to create a workspace object that stores the variables in the base workspace.

    wBase = matlab.lang.Workspace.baseWorkspace
    wBase = 
    Workspace with variables:
    
        Name    Size     Class 
        ____    ____    _______
    
         a      1x1     double 
         b      1x1     logical
         c      1x1     string 
    
    

    Create a copy of that workspace object.

    wCopy = matlab.lang.Workspace(Source=wBase)
    wCopy = 
    Workspace with variables:
    
        Name    Size     Class 
        ____    ____    _______
    
         a      1x1     double 
         b      1x1     logical
         c      1x1     string 
    
    

    Create several variables in the base workspace.

    a = 1;
    b = true;
    c = "Hello, World";

    Use matlab.lang.Workspace.baseWorkspace to create a workspace object that stores the variables in the base workspace.

    wBase = matlab.lang.Workspace.baseWorkspace
    wBase = 
    Workspace with variables:
    
        Name    Size     Class 
        ____    ____    _______
    
         a      1x1     double 
         b      1x1     logical
         c      1x1     string 
    
    

    Create a copy of that workspace object that includes only the variables a and b.

    wCopy = matlab.lang.Workspace(Source=wBase,Variables=["a" "b"])
    wCopy = 
    Workspace with variables:
    
        Name    Size     Class 
        ____    ____    _______
    
         a      1x1     double 
         b      1x1     logical
    
    

    Version History

    Introduced in R2025a