Course: Introduction to Computing – Matlab Application
Dr.-Ing. Nguyen Van Binh
Email: nvbinh@hcmiu.edu.vn
HCMC, Sep. 2022
Content
1. Introduction to
2. Numeric, Cell, and Structure Arrays
3. Functions and Scripts
4. Programming with Matlab
5. GUI with Matlab
6. Other engineering topics
2 September 2022 Introduction to Computing – Matlab Application NV Binh 2
Introduction to Computing
Documents & References
Lecture notes
Stephen J. Chapman - MATLAB
Programming with Applications for
Engineers, Cengage Learning, 2013
William J. Palm, Introduction to Matlab for
Engineers, McGraw-Hill, 2010
https://blackboard.hcmiu.edu.vn/
2 September 2022 Introduction to Computing – Matlab Application NV Binh 3
1 Introduction to Matlab
1.1 MATLAB
• MATLAB (MATrix LABoratory)
• Special-purpose computer program optimized to perform engineering and
scientific calculations
• Provides a very extensive library of pre-defined functions to make technical
programming tasks easier and more efficient
• The built-in MATLAB functions are almost always better than anything that an
individual engineer could write
• Much easier to solve technical problems in MATLAB
• The MATLAB language is a procedural programming language, meaning that the
engineer writes procedures, which are effectively mathematical recipes for
solving a problem.
2 September 2022 Introduction to Computing – Matlab Application NV Binh 4
1 Introduction to Matlab
1.2 Advantages
• Ease of Use: Programs may be easily written and modified with the built-in
integrated development environment and can be debugged with the MATLAB
debugger.
• Platform Independence: supported on many different computer systems,
providing a large measure of platform independence
• Predefined Functions - Many special-purpose toolboxes
• Device-Independent Plotting: integral plotting and imaging commands
• Graphical User Interface: allow a engineer to interactively construct a graphical
user interface (GUI)
• MATLAB Compiler: compiling MATLAB programs into a device-independent p-
code and then interpreting the p-code instructions at run-time.
2 September 2022 Introduction to Computing – Matlab Application NV Binh 5
1 Introduction to Matlab
1.3 MATLAB Environment
• Fundamental unit of data in any MATLAB program is the array
• An array is a collection of data values organized into rows and columns
• When MATLAB executes, it can display several types of windows that accept
commands or display information.
• The three most important types of windows:
Command Windows, where commands may be entered;
Figure Windows, which display plots and graphs;
Edit Windows, which permit a user to create and modify MATLAB
programs.
• Display other windows that provide help and that allow the user to examine the
values of variables defined in memory.
2 September 2022 Introduction to Computing – Matlab Application NV Binh 6
1 Introduction to Matlab
1.3 MATLAB Environment
2 September 2022 Introduction to Computing – Matlab Application NV Binh 7
1 Introduction to Matlab
1.3 MATLAB Environment
2 September 2022 Introduction to Computing – Matlab Application NV Binh 8
Course: Introduction to Computing – Matlab Application
Dr.-Ing. Nguyen Van Binh
Email: nvbinh@hcmiu.edu.vn
HCMC, Sep. 2022
Content
1. Introduction to
2. Numeric, Cell, and Structure Arrays
3. Functions and Scripts
4. Programming with Matlab
5. GUI with Matlab
6. Other engineering topics
2 September 2022 Introduction to Computing – Matlab Application 2
2 Numeric, Cell, and Structure Arrays
2.1 Variables and Arrays
• Individual data values within an array are accessed by including the name of the
array followed by subscripts in parentheses that identify the row and column of
the particular value.
• Arrays can be classified as either
vectors - one dimension
or matrices - two or more
dimensions
• The size of an array is specified
by the number of rows and the
number of columns in the array,
with the number of rows mentioned first.
2 September 2022 Introduction to Computing – Matlab Application 3
2 Numeric, Cell, and Structure Arrays
2.1 Variables and Arrays
• Individual elements in an array are addressed by the array name followed by
the row and column of the particular element.
• A MATLAB variable is a region of memory containing an array, which is known
by a user-specified name.
• MATLAB variable names must begin with a letter, followed by any combination
of letters, numbers, and the underscore (_) character.
2 September 2022 Introduction to Computing – Matlab Application 4
2 Numeric, Cell, and Structure Arrays
2.1 Variables and Arrays
• The most common types of MATLAB variables are double and char.
• The real and imaginary components of each variable can be positive or negative
numbers in the range 10308 to 10-308
• A = 10.4
• B = 12.8j
• C = 10.4 + 12.8j
2 September 2022 Introduction to Computing – Matlab Application 5
2 Numeric, Cell, and Structure Arrays
2.2 Creating and Initializing Variables
• var = expression;
• temp = 40j;
• Temp = temp/4;
• Matrix = [1 4 7; 6 4 2];
2 September 2022 Introduction to Computing – Matlab Application 6
2 Numeric, Cell, and Structure Arrays
2.2 Creating and Initializing Variables
• With algebraic operations
• specific array element is defined
• specifying a extended value
2 September 2022 Introduction to Computing – Matlab Application 7
2 Numeric, Cell, and Structure Arrays
2.2 Creating and Initializing Variables
• Colon operator.
• with the transpose operator (')
2 September 2022 Introduction to Computing – Matlab Application 8
2 Numeric, Cell, and Structure Arrays
2.2 Creating and Initializing Variables
• Initializing with Built-In Functions
2 September 2022 Introduction to Computing – Matlab Application 9
2 Numeric, Cell, and Structure Arrays
2.3 Multidimensional Arrays
• create a 2 x 3 x 2
2 September 2022 Introduction to Computing – Matlab Application 10
2 Numeric, Cell, and Structure Arrays
2.4 Special Values
2 September 2022 Introduction to Computing – Matlab Application 11
2 Numeric, Cell, and Structure Arrays
2.4 Displaying Output Data
2 September 2022 Introduction to Computing – Matlab Application 12
2 Numeric, Cell, and Structure Arrays
2.4 Displaying Output Data
2 September 2022 Introduction to Computing – Matlab Application 13
2 Numeric, Cell, and Structure Arrays
2.4 Displaying Output Data
Formatted Output with the fprintf function
fprintf(format, data)
fprintf('The value of pi is %f \n',pi)
fprintf('The value of pi is %6.2f \n',pi)
2 September 2022 Introduction to Computing – Matlab Application 14
2 Numeric, Cell, and Structure Arrays
2.5 Data Files
save filename var1 var2 var3
load filename
2 September 2022 Introduction to Computing – Matlab Application 15
2 Numeric, Cell, and Structure Arrays
2.6 Scalar Operation
the number of rows and columns in both arrays must be the same.
2 September 2022 Introduction to Computing – Matlab Application 16
2 Numeric, Cell, and Structure Arrays
2.6 Scalar Operation
2 September 2022 Introduction to Computing – Matlab Application 17
2 Numeric, Cell, and Structure Arrays
2.7 Built-In MATLAB Functions
2 September 2022 Introduction to Computing – Matlab Application 18
2 Numeric, Cell, and Structure Arrays
2.7 Built-In MATLAB Functions
2 September 2022 Introduction to Computing – Matlab Application 19
2 Numeric, Cell, and Structure Arrays
2.8 Plotting
M-file create
2 September 2022 Introduction to Computing – Matlab Application 20
2 Numeric, Cell, and Structure Arrays
2.8 Plotting
Multi-plot
2 September 2022 Introduction to Computing – Matlab Application 21
2 Numeric, Cell, and Structure Arrays
2.8 Plotting
Plot command parameters
Help plot
2 September 2022 Introduction to Computing – Matlab Application 22
2 Numeric, Cell, and Structure Arrays
2.8 Plotting
Legend position
2 September 2022 Introduction to Computing – Matlab Application 23
2 Numeric, Cell, and Structure Arrays
2.8 Plotting
Legend position
2 September 2022 Introduction to Computing – Matlab Application 24