Scope Variables and Generate Names
R2026bEach function has its own workspace, which is separate from the base workspace that is reserved for scripts and the command line. For the preferred ways to pass data between functions, see Share Data Between Workspaces.
Functions
Topics
- Base and Function Workspaces
The base workspace stores variables that you create at the command line or by running scripts in the Editor. To protect data integrity, every function has its own workspace that is separate from the base workspace and all other workspaces.
- Check Variable Scope in Editor
Use MATLAB® function and variable highlighting features to identify when and where your code uses a particular function or variable.
- Variable Names
Create valid variable names and avoid potential naming conflicts.
- Transparency in MATLAB Code
Write code that MATLAB can optimize more effectively.
Troubleshooting
Resolve Error: Attempt to Add Variable to a Static Workspace.
Troubleshoot errors when attempting to add variables to nested and anonymous functions. Variables used within nested and anonymous functions must be present in the text of the code. If you add a variable to the workspace of an anonymous function, a nested function, or a function that contains a nested function dynamically, MATLAB issues an error.
Teaching Resources
Programming: Structuring Code
Learn how to write and document functions, and how to debug, comment on, and share your code.