MATLAB Workspace Basics
1. How to Access MATLAB
You can access MATLAB directly from your local computer (suggested) or from a standard web browser with MATLAB Online. To access MATLAB you must have a MathWorks Account and a MATLAB license (usually obtained through your university).
Instructions on installing MATLAB
Instructions on accessing MATLAB Online
2. MATLAB Interface:
When you first open MATLAB, you will notice that it has several windows and a ribbon of buttons at the top. The window on the left is the Current Folder, the central window is the Command Window, and the upper-right window is the Workspace.
Current Folder: Shows a list of all the folders located within the folder you are currently in. (This is essentially your window.)
Command Window: The main area where you can type commands directly and see their output.
Editor Window: Where you writing, running, and debugging MATLAB code
Workspace Window: Displays variables created during the session, providing an overview of the data you are working with.
3. Customization and Setting Preferences
MATLAB offers flexible customization options to optimize your workflow. Some ways to tailor the interface include...
- Resize and Reposition Windows: Adjust the size and position of MATLAB windows by clicking and dragging the window dividers or title bars to suit your preferences.
- Save Frequently Used Commands : The Favorites menu allows you to bookmark commonly used commands. Add a command by selecting 'New Favorite,' entering the command, and giving it a descriptive name. For quicker access, add these favorites to the Quick Access toolbar and execute them with a single click.
- Display Variable Details: To gain more insights into the variables you're working with, such as their types or memory usage, right-click the header bar in the Workspace window and choose the properties you wish to display.
For more advanced customizations, navigate to Preferences under the Home tab.
4. Search Path and Directories:
A directory is a location in the file system that can contain files and other directories (subdirectories). It's essentially a folder where files and other folders are organized.
MATLAB organizes folders and files using a directory tree, starting with the root directory (/
).
A path is a string that specifies the location of a file or directory in the file system. It can be an absolute path (starting from the root of the file system) or a relative path (relative to the current directory)
- Absolute Path: Specifies the entire path from the root directory. E.g.,
/home/user/Documents/file.txt
- Relative Path: Specifies the path relative to the current directory. E.g.,
Documents/file.txt
if the current directory is /home/user.
The search path is a subset of all the folders in the file system that MATLAB uses to locate files efficiently. The order of folders on the search path is significant: MATLAB uses the file in the folder nearest to the top when multiple files with the same name exist. For more information, see What Is the MATLAB Search Path?.
5. Command Cheat Sheet
Commands | Description |
---|---|
Directories and Paths | |
pwd |
Prints the current working directory path. |
cd 'folder' |
Changes the current working directory to 'folder'. |
cd ~ |
Changes the current directory to the user's home directory. |
cd . |
Refers to the current directory. |
cd .. |
Moves up one level in the directory tree. |
ls |
Lists all files and directories in the current directory. |
dir |
Lists directory contents, similar to ls . |
what |
Lists all MATLAB specific files in the current directory. |
mkdir 'dirname' |
Creates a new directory named 'dirname'. |
rmdir 'dirname' |
Removes the directory named 'dirname', if it is empty. |
path |
Displays the current MATLAB search path. |
addpath 'folder' |
Adds the specified folder to the MATLAB search path. |
rmpath 'folder' |
Removes the specified folder from the MATLAB search path. |
savepath |
Saves the current MATLAB search path for future sessions. |
Files | |
exist 'name' |
Checks if a variable, file, or folder 'name' exists. |
load 'filename' |
Loads variables from a file into the workspace. |
save 'filename' |
Saves workspace variables to a file. |
type 'filename' |
Displays the contents of the specified file. |
which 'filename' |
Returns the full path if the file is on the search path. |
edit 'filename' |
Opens the specified file in MATLAB's editor. |
Command Help | |
doc command |
Opens the documentation for 'command'. |
help command |
Displays help text for 'command' in the Command Window. |
clc |
Clears all input and output from the Command Window, keeping history. |
quit or exit |
Closes MATLAB. |
Managing Workspace | |
clc |
Clears all input and output from the Command Window, keeping history. |
clear |
Clears all input and output from the Command Window, keeping history. |
quit or exit |
Closes MATLAB. |
6. Keyboard Shortcuts for MATLAB Command Window
Keyboard shortcuts in MATLAB's Command Window mirror the functionality often used in terminal environments.
Note - These shortcuts will only work if you have MATLAB installed in your system. MATLAB Online provides access to MATLAB from a standard web browser. Because MATLAB Online runs in a browser, navigation using the keyboard is slightly different.
For Windows Users
Shortcut | Description |
---|---|
Up Arrow |
Scrolls through the history to recall previous commands, useful for repetition or modification. |
Down Arrow |
Scrolls forward in the command history after using the Up Arrow. |
Home or Ctrl + A |
Jumps to the beginning of the current line. |
End or Ctrl + E |
Jumps to the end of the current line. |
Tab |
Auto-completes commands and variable names based on partially typed text. |
Backspace |
Deletes the character to the left of the cursor. |
Delete |
Deletes the character right at the cursor position. |
Enter |
Executes the command currently typed in the Command Window. |
Shift + Enter |
Continues command entry onto the next line for multi-line commands. |
Ctrl + L |
Clears the screen, keeping the history intact. |
Esc |
Cancels the current command line input without executing it. |
Ctrl + C |
Interrupts the execution of the current command or script. |
For Mac Users
Shortcut | Description |
---|---|
Up Arrow |
Scrolls through the history to recall previous commands. |
Down Arrow |
Scrolls forward in the command history after using the Up Arrow. |
Cmd + Left Arrow or Ctrl + A |
Mimics the Home key, jumping to the beginning of the line. |
Cmd + Right Arrow or Ctrl + E |
Mimics the End key, jumping to the end of the line. |
Tab |
Auto-completes commands and variable names based on partially typed text. |
Delete |
Deletes the character to the left of the cursor. (Mac keyboards typically do not have a separate Backspace key.) |
Fn + Delete |
Deletes the character right at the cursor position. |
Return |
Executes the command currently typed in the Command Window. |
Shift + Return |
Continues command entry onto the next line for multi-line commands. |
Cmd + K |
Clears the screen, keeping the history intact. |
Esc |
Cancels the current command line input without executing it. |
Ctrl + C |
Interrupts the execution of the current command or script. |
7. Suggested Tutorials
8. Supplemental Resources
Documentation: Set Command Window Preferences