1. Introduction to MATLAB (1)
1. Introduction to MATLAB (1)
Introduction to MATLAB
2.1.1 What is MATLAB?
Once the program starts, the window that opens, is shown in the following figure.
Current folder
Editor Window
Workspace
Command Window
Command History
Figure 1: MATLAB desktop environment
The figure 1 contains five smaller windows which are the command window, the workspace window,
the current folder window, the command history window and the editor window. A list of several
windows and their purpose is given in table 1 below.
Table 1: MATLAB windows
Window Purpose
Command window Main window, enter variables, runs programs.
Figure window Contains output from graphic commands.
Editor window Creates and debugs script and function files.
Help window Provides help information.
Command history window Logs commands entered in the command window.
Workspace window Provides information about the variable that are used
Current folder window Show the files in the current working directory.
Command window: The command window is MATLAB’s main window, and opens when MATLAB is
started. This is the main area where commands can be entered at the command line. An example
of an editor window is shown in following figure 1
Editor window: The editor window is used for writing and editing programs. This window is opened
from the file menu. An example of an editor window is shown in following figure 1
Figure window: The figure window opens automatically when graphic commands are executed, and
contain graphs created by these commands. An example of a figure window is shown in following
figure 2.
Help window: The help window contains help information. This window can be opened from the
help menu in the toolbar of any MATLAB window. Help window is interactive and can be used to
obtain information on any feature of MATLAB. Help window is shown following figure 3 below
Figure 3: Help window
• To type a command, the cursor must be placed next to the command prompt (>>).
• Once a command is typed and the enter key is pressed, the command is executed. However,
only the last command is executed. Everything executed previously is unchanged.
• Several commands can be typed in the same line. This is done by typing a comma between
the commands. When the enter key is pressed the commands are executed in order from
left to right.
• It is not possible to go back to a previous line in the command window, make a correction,
and then re-execute the command.
• A previously typed command can be recalled to the command prompt with the up arrow
key (↑). When the command is displayed at the command prompt, it can be modified if
needed and executed. The down arrow key (↓) can be used to move down previously
typed command.
• If a command is too long to fit in one line, it can be continued to the next line by leaving
a space and typing three periods … (called an ellipsis) and pressing the enter key. The
continuation of the command is then typed in the new line.
When command is typed in the command window and the enter key is pressed, the command is
executed. Any output that the command generates is displayed in the command window. If
semicolon (;) is typed at the end of a command. The output of command is not displayed.
Percent symbol (%):
When the symbol % is typed in the beginning of a line, the line is designated as a comment. This
means that when the enter key is pressed the line is not executed.
The clc command:
The “clc” command (type clc and press Enter) clears the command window. After working in the
command Window for a while, the display may be very long. Once the “clc” command is executed
a clear window is displayed. The command does not change anything that was done before. For
example, if some variables were defined previously, they still exist and can be used.
1/3 is executed first, 64^(1/3) and 32 ^0.2 are executed next, and + is
executed the last
The left-hand side of the assignment operator can include only one variable name. The right-hand
side can be a number, or a computable expression that can include numbers and/or variables that
were previously assigned numerical values. When the Enter key is pressed the numerical value of
the right-hand side is assigned to the variable, and MATLAB displays the variable and its assigned
value in the next two lines.
The following shows how the assignment operator works.
Assign 12 to a
Assign 4 to B
• If a semicolon is typed at the end of the command then, when the enter key is pressed,
MATLAB does not display the variable with its assigned value (the variable still exists and
stored in memory).
• If a variable already exists, typing the variable’s name and pressing the enter will display the
variable and its value in the next lines.
The left-hand side of the assignment operator can include only one variable name. The right-hand
side can be a number, or a computable expression that can include numbers and/or variables that
were previously assigned numerical values. When the enter key is pressed the numerical value of
the right-hand side is assigned to the variable.
Rules about variable names
• Variable name can contain letters, digits and the underscore character.
• Variable name must begin with a letter.
• Variable name cannot contain punctuation characters
• MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. For
example, AA, Aa, aA and aa are the names of four different variables.
• No spaces are allowed between characters (use the underscore where a space is desired).
• Avoid using the name of built in function (avoid using cos, sin, exp, sqrt)
Predefined variables
A number of frequently used variables are already defined when MATLAB is started. Some of the
predefined variables are:
If the user does not assign the value of an expression to variable, MATLAB automatically
ans
stores the result in ans
pi The number π
eps The smallest difference between two numbers.
Inf Used for infinity.
i Defined as √−1 , which is 0 + 1.000𝑖
j Same as i
NaN Stands for not a number.
2.1.9 Useful Commands for Managing Variables
Command Outcome
clear Removes all variables from memory
clear x y z Removes only variable x, y and z from the memory
who Display a list of variables currently in the memory
Display a list of the variables currently in the memory and their size together with
whos
information about their bytes and class.
The help command can be used to find more about a particular command.
help
Ex: help ex