What is MATLAB?
MATLAB (a contraction of the words "Matrix Laboratory") is a high-level programming language. The MATLAB environment enables intensive computation of tasks faster than traditional programming languages like C, C++ and Fortran, and specialises in numerical matrix calculations and manipulations.
The MATLAB Environment
In this chapter we will assume that you have already installed the MATLAB software on your PC or Mac (note that there's also an online version). Throughout the course, we will be using version
2017a. In order to get familiar with the MATLAB environment, we're going to go through the following parts of the program step by step:
The Command Window
The Command Window – also known as the command line – allows us to type simple commands and view their results instantly. For example, to add two integers, enter the following command:
We can then create a variable called
ans and store the result of the arithmetic sum in the memory space allocated to it. Note that the variable is also automatically added to our Workspace Window.
Note: the Command Window can be cleared by typing the command
The File Editor Window
If we have a long collection of commands, we're going to want to save them as "m-files" (which are basically just text files with a
.m-extension). To create these m-files, we'll use the File Editor – a word processor designed specifically for MATLAB commands. The File Editor automatically formats your text and commands based on the MATLAB guidelines, which we'll look at in an upcoming chapter.
To create a new file, click the "New"-button located in the top left of the MATLAB window, and select the "script" option in the dropdown list.
You can also use the shortcut Ctrl + N (cmd + N on a Mac) or type
edit in the Command Window.
In the File Editor Window, type the following code:
As a convention, the names given to your m-files should never contain spaces or special characters except for underscores. Click on the Save-button in the task bar and save your program as
code_one. Now you can run the script by clicking on the Run-button in the task bar, or simply by typing the name of the script in the command window:
Here's what you should see in your File Editor and in the Command Window once you run
The Workspace Window
The Workspace Window is where all your variables are displayed when a code is executed. It displays the variable's name and its attributes, such as its minimum and maximum values, and the class that it belongs to.
MATLAB Help is a powerful learning tool that you should use whenever you need assistance. It contains both the theory behind certain concepts, as well as demos for how to implement them. MATLAB Help can be opened by clicking the question mark (
?) button in the upper right corner. Alternatively, you can type the following command in the command window:
Using this documentation, we can easily look up any MATLAB function. If you already know the exact name of the function you're looking for, you can also use the command window:
The information about the
sum-function is then printed out directly in the command window:
The Current Folder Window
The Current Folder (a.k.a. "working directory") is a path directory to the folder or directory in which you are working. In MATLAB, it simply lists all the files that are available in that folder.
The Command History
The Command History stores all the recent commands that have been executed in the Command Window in chronological order. You can also navigate through this list in the command window by using your keyboard.
If you're still a bit confused about how the MATLAB environment works, you can have a look at this video by Vanderbilt University below where they explain everything clearly.
To get acquainted with MATLAB's commands, feel free to try out the following commands one by one in the Command Window. Make sure that you pay attention to how the workspace, current folder, command window and command history change each time you execute a command.