matlab.lang.Workspace - Store workspace variables - MATLAB (original) (raw)
Main Content
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 amatlab.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 thematlab.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.
Syntax
Description
[w](#mw%5F460ceb3b-17d2-44b6-966a-c0c5c8f215bd) = matlab.lang.Workspace
creates an empty workspace object.
[w](#mw%5F460ceb3b-17d2-44b6-966a-c0c5c8f215bd) = matlab.lang.Workspace(Source=[sourceWorkspace](#mw%5F9d7eb5b8-784c-414f-9eb8-2017c479e3f4))
creates a workspace object that contains a copy of variables from the specified workspace.
[w](#mw%5F460ceb3b-17d2-44b6-966a-c0c5c8f215bd) = matlab.lang.Workspace(Source=[sourceWorkspace](#mw%5F9d7eb5b8-784c-414f-9eb8-2017c479e3f4),Variables=[varList](#mw%5F73a28584-6cd1-4c38-bf63-31e23df15633))
creates a workspace object that contains a copy of the specified variables from the specified workspace.
Input Arguments
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
Workspace, returned as a matlab.lang.Workspace
object.
Object Functions
Examples
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.
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.
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