Getting Started with MATLAB
MATLAB Software
MATLAB Interface
MATLAB opens showing multiple interface panes. In general, you need to see the Command Window and the Workspace. Other visible panes may include the Command History and Current Folder. If you want to change the visible panes, click on the Layout button from the HOME tab and select panes to add or hide.
MATLAB Online is similar, and you can customize the same way as the Desktop app.
Get Help in MATLAB
- Use the
help
function to display help text for a specified MATLAB function. - Use the
doc
function to open the MATLAB Help Browser.
Set Current Folder
MATLAB looks in the current folder to find your script and function files. You need to set the current folder to match the location of the script or function that you want to run. You should get in the habit of saving your files to your Purdue Career Account drive, which is backed up by Purdue servers, or to MATLAB Drive, which is backed up by MathWorks. Using a basic folder structure to organize your files by assignment, exam, and projects is a good engineering habit and will help you when you need to navigate in MATLAB to the correct folder.
MATLAB Basics
Tutorials and help documentation from MathWorks.
Variable Names
MATLAB variable names in this course must meet both the MATLAB requirements and the course programming standards. The definition of a built-in function in this course is any command that came pre-loaded in MATLAB.
Rule for Naming Variables | Example |
---|---|
Must start with a letter | Correct: time1 Incorrect: 1time |
Can only contain letters, numbers, and/or underscores | Correct: travel_time Incorrect: travel-time |
Should be descriptive of the data | Descriptive: travel_time Inappropriate: t |
Are case sensitive | DIST is the not the same variable as dist |
Cannot be MATLAB keywords | Examples: break , if Type iskeyword in the Command Window to view a list of MATLAB keywords |
Do not use built-in function names | Examples: clc , sqrt , min , max MATLAB will allow you to create a variable name with the same name as a built-in function. Avoid doing this because doing so is bag programming and may cause unexpected behavior when you execute your code. |
Useful MATLAB commands to help discover existing variable and function names
Command Syntax | Functionality |
---|---|
who | List the existing variables in the current workspace |
whos | List the existing variable with their formats in the current workspace |
help elfun | View a list of elementary math functions |
help possible_varname | See if there is help information for a built-in function. The function exists if this command returns help documentation for the requested function name |
exist possible_varname | Check if variable names or functions are defined. Example returns: 0 if possible_varname does not exist1 if it is in the workspace 5 if it is a built-in function |
possible_varname | Enter the desired variable name into the Command Window. This returns an error if MATLAB does not recognize the name as a variable, function, etc. |