Introduction (Matlab)
MATLAB (Matrix Laboratory) is a high level programming language that can perform operations on values, symbols, and matrices. It is commonly used on various technical applications such as signal processing, controls, etc. This tutorial will focus on the basics of MATLAB and how to use it. A more advanced reference can be found at Intermediate Skills.
Contents
Accessing MATLAB at WPI
There are various ways to use MATLAB at WPI. Most school computers, including those at Atwater Kent and Gordon Library, have MATLAB installed. Students can no longer download the software from WPI due to matlab license limitations Sorry.
Main Screen
When MATLAB is first opened and loaded, the screen will look similar to the one below:
At the top are various buttons with different purposes, such as creating and opening scripts, importing data, opening Simulink, etc.
To the left side of the window is the current folder. This shows a list of files and folders in your current directory, which can be navigated by clicking on folders or using the bar below the various buttons. Note: If MATLAB can’t find a function or script that you created, use this to check that you are in the correct directory.
In the center of the screen is the Command Window. Here is where the user types commands. Lines are typed one at a time, but semicolons can be used to add multiple functions to each line.
The Workspace window on the center right is where variables and their current values are shown. If the variables represent large vectors or matrices, the window shows their dimensions instead of individual values.
The Command History window in the bottom right shows commands that were typed recently, with the most recent command at the bottom. These commands can be clicked and rerun instead of being retyped.
Basic Commands
To run a command, type it into the Command Window and press Enter. Note: adding a semicolon at the end of a line prevents the program from displaying the result on the Command Window.
- Help
- Doc
- Doc is similar to help, but it opens a window displaying information. This usually includes more information than help, such as examples.
- clear
- deletes all variables in the workspace
- clc
- empties the command window
- close all
- closes all windows opened by MATLAB such as figures and documentation. Can also close individual figures and GUIs.
Calculations
MATLAB can perform arithmetic calculations (+, -, *, /, ()) between values and obeys the order of operations.
Booleans and Logic
Like other programming languages, MATLAB has if, else statements which use Booleans, such as the ones shown below:
- && (and)
- || (or)
- ~= (not equal to)
- >,<, == (greater than/less than/ equal to)
MATLAB does not use brackets or indentation to indicate the end of an if statement. Instead, it uses the “end” command. See the example below.
Loops
MATLAB has two commonly used types of loops: for and while.
This for loop iterates over the vector [1 2 3 4 5] in which the current value is represented by v. This for loop simply displays v in each iteration.
Vectors and Matrices
A vector is a 1-by-X set of numbers in which X is any positive integer. Vectors can be created from scratch or, if they increment or decrement by a fixed value, by using the minimum, increment, and maximum value:
Note: If a vector increments by 1, the middle value is not needed. The vector will increment by 1 by default.
A matrix is an X-by-Y set of values in which X and Y are positive integers. They are made similarly to vectors:
Note that semicolons indicate the end of a row. Also, every row must have the same number of elements. Same with every column.
Note: Remember that a vector is a matrix in which X or Y is 1.
Matrix Operations
There are various functions for operating on vectors and matrices.
To find a certain element in a matrix, call the name of the matrix with the row and column in parentheses:
To find the transpose of a matrix, simply use “ ‘ ” as shown below:
Functions such as min and max can be used to find the minimum and maximum values of each column in a matrix. The “inv(matrix)” command can find the inverse of a given matrix. MATLAB can also perform matrix multiplication, including dot products:
Plots
MATLAB can plot points of two vectors as line graphs using “plot(X,Y)” and scatterplots using “scatter(X,Y),” in which X and Y are two vectors with the same length.
If you try to plot two graphs one after the other, the second will replace the first. If you want both lines on the same graph, use the command “hold on” between them. If you want it the second plot as a separate figure, use “figure(X)” in which X is the figure name. Plots have a variety of commands for customization:
- xlabel('string')
- label x axis
- ylabel('string')
- label y axis
- title('string')
- add title to the graph
- axis([xmin xmax ymin ymax])
- set axes of graphs
Scripts and Functions
Scripts
Scripts are a series of MATLAB commands that are run one right after the other. They are faster and easier to debug and save than writing individual commands.
To create a script, click “New Script” in the upper-left corner of the MATLAB window or press “Ctrl+N.” When the script is open, type out the commands and click “Run” or type the name of the script in the command window.
Functions
Functions are very similar to scripts, except that they can have inputs and outputs. A function can have multiple inputs and outputs and must start with the word “function.” Note: A function name must be the same as the file’s name.
Unlike other programming languages, no return command is needed to return a variable. Instead, the function returns the latest value of the variable. Note: If a function has multiple outputs, make sure you create multiple variables in the output in the form of a vector (shown above in red circle). Otherwise, only the first output will be stored.
More Help
Should you need more, please go to the Matlab reference page on Intermediate Skills.
Furthermore, the following tutorials and sites have proven helpful to other students. Please follow the links if you require more assistance.
- Basics of MATLAB Slideshow and associated source code: another quick MATLAB introduction by Adriana Hera at WPI.