Matlab Desktop

Unlike most programming languages, MATLAB’s Desktop application is central to the experience — although it is not strictly required. For example, you can use the MATLAB Engine API for Python to drive an embedded MATLAB terminal inside VSCode in place of the Desktop.

MATLAB Desktop (R2020b) includes 3 panels:

  1. Current Folder — Access your files
  2. Command Window — Enter commands at the command line, indicated by the prompt >>
  3. Workspace — Explore data that you create or import from files.

The first two are similar to the command prompt of any language; the distinctive panel is the Workspace, which represents the working memory and shows every variable in the current project.

Command Window

  • Like the interactive REPL of other languages, the Command Window is suited to running short, throwaway code and shows the results
  • You can run a file.m from the Current Folder by typing file
    • Do not include the extension
    • You cannot run a file from the system shell with matlab file.m
    • You can also run a file with run relative/or/absolute/path.m
  • Expressions without an assignment are bound to the default variable ans
    • ans is also stored in the Workspace
  • For long commands, soft-wrap with ... followed by Enter
    • ... is treated as a space at execution time
    • Code after ... on the same line is ignored, so a comment can follow it
  • Append ; to suppress the result display for an individual command

⭐ Common Commands

commanddescription
format [short]Display doubles in short format (5 decimal places)
format longDisplay doubles in long format
format short eShort scientific notation
format long eLong scientific notation
format bankShow 2 decimal places
format hexHexadecimal representation
format ratApproximate rational representation
format +Show only the sign of each element
dbstop if errorStop at the error site and preserve all relevant variables
whoList Workspace variable names
whosList Workspace variables with details
whatList all .m and .mat files in the Current Folder
clcClear Command Window
clearMatlab Functions - clear
save, loadMatlab Functions - save, load
diaryLog following input and output in Command Window to file diary
cdChange directory
deleteDelete file(s)
helpHelp documents
lookforSearch all MATLAB files for keyword
runRun the file with the exact path
quit, exitExit MATLAB
  • These commands are not restricted to the Command Window — they may also appear inside an Matlab M File. They are merely closely associated with the Command Window.

Workspace

  • Workspace variables can be saved to a .mat file and loaded back later
    • If the Workspace already contains a variable with the same name as one in the .mat file, the loaded value overwrites the existing one
    • Use save and load to save and load variables Matlab Functions - save, load
  • The command (function) clear clears all Workspace variables
  • The command pack reorganises memory
  • Double-clicking a variable in the Workspace opens it for editing