06 - Chapter 01 Introduction to MATLAB
06 - Chapter 01 Introduction to MATLAB
Introduction to MATLAB
1.1 Introduction
1.3.1 History
The earliest origin of MATLAB can be traced back to the invention of EISPAC
(Matrix Eigensystem Package) software, which was developed to solve eigenvalue
problems. The fundamental of this software was the procedure followed by ALGOL
60 to solve such problems. It was first developed at the Argonne National Laboratory
around 1970, and the first version of the software was released in 1971, followed by
the release of the second update in 1976. Later, another software for mathematical
analysis named LINPACK (Linear Equation Package) was developed as a byproduct
in 1975 in the same lab by Cleve Moler, Jack Dongarra, Pete Stewart, and Jim
Bunch. EISPACK and LINPACK both were formed in Fortran and can be consid-
ered as the very primal stage before the appearance of MATLAB.
Although EISPACK and LINPACK were capable of performing numerical
analysis and solving linear algebra problems, Moler wanted to upgrade both of
them to reduce the complexity of access for his students. With that aim, Moler
first came up with the idea of MATLAB, which was named after the Matrix
Laboratory. MATLAB was simply a matrix calculator, where the data type of
input was matrix. This version was created by Moler only for the usage of his
students, which is later regarded as classical MATLAB.
The idea of the first commercial MATLAB was proposed by a graduate student of
Stanford named Jack Little in 1983. Jack Little, Steve Bangert, and Cleve Moler took
the initiative to bring IBM PC-based MATLAB, which was translated in C from
Fortran. The first PC-based commercial MATLAB made its appearance in
December 1984, followed by the first marketization in 1985. This new version of
MATLAB was updated and modified significantly by both Jack Little and Steve
Bangert, where they added many mathematical functions, graphics, and toolboxes
based on various applications. MATLAB has been upgraded multiple times for
adjusting itself with the new applications and requirements in the domain of engi-
neering and science. The major changes that have occurred over time in different
versions of MATLAB are summarized in Table 1.1.
1.3 History, Purpose, and Importance 3
Step 3: Find the “Advanced Option” dropdown box, and choose the option “I have a
File Installation Key.” This is shown in Fig. 1.2.
Step 4: Enter the key.
Step 5: Navigate to the license file location.
Step 6: Navigate to the directory, where MATLAB will be installed.
Step 7: Choose the products for the installation.
Step 8: Choose the options that are convenient.
Step 9: Click on the “Begin Install” option, and wait for a while.
Step 10: After the installation is completed, a “Finish” option will appear. By
clicking on it, finalize the installation of MATLAB.
Method 3: Download Installation Package
Step 1: Download the installation file and run the installer.
Step 2: MathWorks sign-in option will appear; hence, sign in to the account. From
the dropdown “Advanced Options,” select the “I want to download without
installing” option.
Step 3: Navigate to the directory where MATLAB will be installed.
Step 4: Choose the platform of installation and also decide the products that are
relevant to be installed.
Step 5: After proceeding further, a “Begin Download” option will appear. Click on
that option, and wait for a while.
Step 6: When the download will be finished, click on the “Finish” button, which will
appear right after the completion of the download.
Step 7: Move the file to the desired destination, and run the installer file. For
windows, the installer file will be found as “setup.exe.” For Linux and MAC,
the name of the installer file will be “install” and “InstallForMacOSX,”
respectively.
Step 8: After running the installer file, follow Method 1, if the Internet connection is
available. Otherwise, follow Method 2, if a file activation key is available for
completing the installation.
6 1 Introduction to MATLAB
1.4.1 Dependencies
Windows: Open the program menu and find the MATLAB application file (matlab.
exe) from the directory. Double-click on it to start the MATLAB.
Linux: Open the terminal and write “matlab.” After pressing enter, the MATLAB
will start.
Mac: On the dock, find the MATLAB icon. By clicking on it, MATLAB can be
opened.
In the MATLAB environment, there are multiple windows that can be set as the
starting layout. In a starting layout, the basic windows that are docked in default
form are Command Window, Editor, Current Folder, and Workspace. In the header
tool strip of MATLAB, the “Layout” option is available, by clicking on which the
layout can be changed according to our convenience. In Fig. 1.3, the layout of the
MATLAB starter page in default mode is shown. The “Command Window” pro-
vides a window where any MATLAB command can be written for execution. The
command can be run by pressing the “Enter” button on the keyboard. For writing a
long program, the command window probably is not the best place. For that, the
“Editor” window is available, where a complete program can be written and run for
execution. The output of the program will appear on the command window. In the
default mode, there are also two other windows: Current Folder or Directory and
Workspace. The current directory indicates the directory from where any MATLAB
file can be exported or imported. In the workspace, all the variables defined in the
command window, or the editor, will appear along with their values after the
execution of the program. From the “Layout” menu, “Command History” window
can also be docked in the starter page, which lists all the previous commands in an
ordered manner. Therefore, if any previous command needs to be reused, it can be
selected from the command history. By clicking the “Up” and “Down” key of the
keyboard, the previous commands can also be navigated. In MATLAB, there is a
header tool strip, where multiple menus and tools are available, which are also parts
of the MATLAB environment.
The features of MATLAB are crucial in the field of engineering. Some of the most
important features of MATLAB can be listed as follows:
1. High-level programming language
2. Built-in graphics
3. Interactive environment
4. High computational capability
5. Numerous mathematical functions
6. Numerous toolboxes dedicated to separate applications
7. Compatibility with other languages
8. Parallel computing
9. App designing
10. Algorithm formation
11. Hardware interfacing
12. Deployment capability, etc.
One of the important aspects of MATLAB is that it is not limited to one
application area; instead, it covers a wide area of applications. MATLAB 9.10
version has a total of 82 toolboxes dedicated to numerous application fields in
various domains. A list of available toolboxes in different application fields is
given in Table 1.3.
While writing a MATLAB code, variables are used to define different parameters
that are needed to be used in the same program multiple times. In variable definition,
the main task is to assign data against each variable, and this assigned data can be of
1.8 Variables in MATLAB: Categories and Conversion Between Variables 9
different types, such as integer, float, string, etc. When a variable is defined, it can be
observed in the Workspace. The name of the variables can be any letter, or any
combination of multiple letters and numbers, such as “var1.” However, a variable’s
name cannot be started with a number; for example, “1var” is not a valid variable
name. In addition, special characters such as “@,” “#,” “$,” “&,” and “-” cannot be
included in a variable’s name. Underscore can be allowed to be used in the variable’s
name except at the starting place. For example, “var_1” is a valid variable name, but
“_var” is invalid in MATLAB.
The mostly used categories of data types of variables can be listed as follows:
(a) Numeric data type: integer, float (single and double), and logical
(b) Character and string type: character, string, and cell array
(c) Date and time
An example of variable definition for each of the above-mentioned data types is
given below using MATLAB.
The MATLAB code demonstrating different data types is given in Fig. 1.4, and its
output is given in Fig. 1.5.
10 1 Introduction to MATLAB
Output:
In MATLAB whos command can be used to print the details of all the defined
variables stored in the Workspace. More precisely, it provides the name, size, bytes,
class, and attributes of the defined variables. Here, the class indicates the data types
of the variable. For example, the class of var1 is int8, which signifies that var1 is a
positive signed 8-bit integer. The data types of the rest of the variables can also be
found from the above-mentioned output.
After defining a variable, the assigned data of the variable can be reused any time
throughout the program, unless the variable is reassigned with new data, or cleared
by MATLAB command. To erase any defined variable from MATLAB, the clear
command can be utilized. Variables can also be cleared by selecting them in the
Workspace window and selecting the delete option.
• If you want to erase all variables from the MATLAB memory, type
clear or clear all
• To erase a specific variable, say x, type clear x
• To clear two specific variables, say x and y, type clear x y
• To clear only the command window, type clc
1.8.3 Conversions
The data types of the defined variables can be converted in MATLAB. The most
used conversions that are required are number-to-text conversion and text-to-number
conversion. The commands for such conversions are listed in Table 1.4.
A MATLAB example is given below for further illustration.
The MATLAB code demonstrating the conversion of data types is given in Fig. 1.6,
and its output is given in Fig. 1.7.
Output:
Here, all the conversions can be verified by observing the “Class” of all the
variables.
Here, in the first example, a vector a has been defined without using a semicolon.
As an outcome, the value of a is printed in the command window. In the second
example, with the usage of the semicolon, the output is suppressed.
In MATLAB, diary function can be useful while recording a MATLAB session that
creates a file containing the keyboard inputs and the outputs. The MATLAB
command for the usage of the diary function is as follows:
diaryð0 Name0 Þ
Here, 0Name0 indicates the name of the file in which the session will be
recorded.
A MATLAB example is provided in Fig. 1.9 with its output in Fig. 1.10 for
further illustration.
Output:
Here the output including the keyboard inputs is recorded in a text file named
“DiaryFile.txt,” as shown above in the output. To record a particular part of the
MATLAB session, diary on and diary off command placed at the start and the end of
a session, respectively, can be very useful. Later, the diary(‘Name’) command can be
used to define the file name on which the recorded version will be saved.
The output of the MATLAB program can be printed in the command window by
using two MATLAB built-in functions fprintf() and disp(). It provides the users great
flexibility to choose which output they want to print. Both fprintf() and disp()
commands are explained below:
fprintf ð0 text 0 Þ
fprintf ð0 ∖n0 Þ
Here, 0text0 can be any string or character, and var indicates the value of a
variable that is required to be printed. Finally, 0\n0 is used to shift to the
next line.
fprintf() can be used to print both text and numbers in the command window. The
input provided within the bracket will be printed in the command window as a string,
or characters. If the defined value of any variable is required to be printed with text,
the fprintf(0text % d0, var) command can be utilized where %d will be replaced by the
value of the defined variable var. Here var indicates the name of the variable.
However, it is only true if the variable is an integer type. For printing the values of
float type variables, %f is used in place of %d. To shift to a new line, \n is used.
16 1 Introduction to MATLAB
A MATLAB example regarding the usage of the fprintf() is provided in Fig. 1.11
with its output in Fig. 1.12.
Output:
Here, in the first fprintf() command, a text is printed. In the second command, \n is
used to print the next output in a separate line. In the third command, an integer
variable named var is printed with text. Finally, in the fourth command, a float type
variable printing with text is shown.
disp() is another MATLAB command for printing output. The usage of disp()
command is shown below:
dispð0 text 0 Þ
dispðvar Þ
Here, 0text0 can be any string or character, and var indicates the value of a
variable that is required to be printed.
In disp(), a new line shift occurs by default; hence, \n notation, as used in fprintf(),
is not required to use here. To print both strings and variables, it is necessary to
convert the data types of the variables in a string format while using disp(). In the
1.12 Conclusion 17
above example, num2str() function is used to convert the data types of the variable in
string format. An example of the usage of disp() is shown in Fig. 1.13 with its output
in Fig. 1.14 for better understanding.
Output:
1.12 Conclusion
This chapter provides a brief introduction to MATLAB and presents the history,
purpose, and importance of MATLAB. The concept of MATLAB is put forward
such that the chapter can be interesting for a new audience. To help the readers
getting started with MATLAB with a hand-in experience, the chapter provides step-
by-step methods of its installation procedures. The readers are advised to implement
all the examples and coding in MATLAB simultaneously to understand the contents
more rigidly. To accomplish this purpose, this chapter introduces the overall
MATLAB environment along with some fundamental features, so that the readers
can feel comfortable writing MATLAB code while going through the rest of
the book.
18 1 Introduction to MATLAB
Exercise 1
Reference
1. https://www.mathworks.com/help/install/