Introduction to MATLAB &
Variables
Numerical Computation and Data Visualization
EECE 2319
ELEE2319 Numerical Computation and Data Visualization
What is MATLAB?
l MATLAB: MATrix LABoratory.
l Programming language very well suited for numerical computation
and data visualization.
l Specially designed to work with vectors and matrices.
l Useful for science, engineering, economy, among other disciplines.
l Extensively used in the industry for analysis, research, and
development.
l Allows the solution of some complex engineering problems in a
fraction of the time that it would take to code it in other languages.
ELEE2319 Numerical Computation and Data Visualization
Matlab Graphical User Interface
ELEE2319 Numerical Computation and Data Visualization
What is MATLAB?
Upon opening MATLAB, the command window should be
immediately available.
Other useful windows: workspace and command history, accessible
through the Layout option.
Two methods of programming in MATLAB:
1. Command Window: Allows input of commands and execution
in real-time.
2. Editor Window: Allows for writing the necessary code before
executing an entire program (which is known as a script).
This method is accessed through the “New Script” Button
ELEE2319 Numerical Computation and Data Visualization
Command Window
In the Command Window, the MATLAB prompt symbol
(>>) tells the user that the software is ready to receive
interactive commands.
l MATLAB can execute valid code from the Command
Window
• The user must first type the instruction and then hit the
Enter key to see the result
ELEE2319 Numerical Computation and Data Visualization
Workspace
• The Workspace keeps track of all
created variables and their values.
• If unable to see the Workspace
window, go to Home → Layout →
Show Workspace.
• To clear all data (variables and their
values), the command clear can be
input into the prompt.
• To clear individual variables, the
command clear var is input, where
var is replaced with the variable
name to clear.
ELEE2319 Numerical Computation and Data Visualization
MATLAB Variables
l The names of variables must start
with a letter, and can include
letters, digits, and underscores.
• MATLAB is case-sensitive.
• The maximum number of
characters in a variable name must
not exceed the value that the
namelengthmax command returns.
ELEE2319 Numerical Computation and Data Visualization
Editor Window
l The Editor Window is
similar to a Notepad file
(.txt extension).
• Scripts created in the
editor are saved with a
“m” extension.
• The file name of a
MATLAB script should
follow the same rules as
variable names
ELEE2319 Numerical Computation and Data Visualization
Creating a New Script
l An Editor Window can
be accessed by creating
a new MATLAB script,
or opening an already
created one.
l To create a new script,
go to Home → New
Script.
l To open a script, go to
Home → Open, and
browse for the .m file.
ELEE2319 Numerical Computation and Data Visualization
Matlab Scripts
l Instead of typing commands directly into the Command
Window, a series of commands can be typed and grouped
together into a script .m file.
l The grouped commands are considered a code that will
define the behavior of a program to be executed whenever the
user decides to.
ELEE2319 Numerical Computation and Data Visualization
Matlab Scripts
l The program is executed via two methods:
l 1. Typing the script’s file name into the
Command Window (need to make sure that
the Current Folder points to the directory
where the .m file exists).
l 2. Clicking on the Run Test button (found
under Editor →Run).
ELEE2319 Numerical Computation and Data Visualization
Writing and Executing a Program
As in a typical programming language, code in a script file is
executed in a procedural fashion: One command at a time,
from top to bottom across the script.
l The script shown below evaluates the product of x and y
and stores it in z.
l After execution, the result is shown in the Command
Window.
ELEE2319 Numerical Computation and Data Visualization
Semicolons
Semicolons ; can be placed at the end of a command
statement to suppress the resulting output in the Command
Window.
l The figures below show the use of semicolons to suppress
the output in the Command Window for variables x and y.
ELEE2319 Numerical Computation and Data Visualization
Comments
l Comment operators % can be used in script files to
“comment out” lines of text/code, which will be ignored by
the compiler.
l Text after the comment operator in a line will be considered
a comment.
l Text preceding the comment operator in a line will not be
considered a comment.
l Comments in a script file helps in organizing the code for
better readability and provide a description of the overall
program and specific parts of the code.
l Comment operators are not really useful in the Command
Window.
ELEE2319 Numerical Computation and Data Visualization
Comments
ELEE2319 Numerical Computation and Data Visualization
Comments
l Adding the clc and clear commands at the very top of a
script ensures that every time the program executes, the
entire Command Window and Workspace are cleared.
l This is extremely useful in many situations, but it is also
a good idea to stop and think whether there are any
negative consequences in your own application.
ELEE2319 Numerical Computation and Data Visualization
Types of Variables
l There are three types of variables that can be
manipulated in MATLAB:
l Scalars
l Vectors
l Matrices
l The type of a variable is dependent on its size.
ELEE2319 Numerical Computation and Data Visualization
Array of Data in a Variable
l A MATLAB variable is stored as
an array of a specific size.
l An array is a collection of data
values, organized into rows and
columns.
l The size of a variable is specified
by the number of rows and
columns in the array as: n × m
l The array on the right is sized
4×4 (4 rows by 4 columns).
l Pay careful attention to the row
and column indexing.
ELEE2319 Numerical Computation and Data Visualization
Scalars
l A scalar is the most elementary variable in MATLAB.
l A scalar is an array always sized at 1×1.
l Hence, a scalar only holds 1 row and 1 column of
data.
ELEE2319 Numerical Computation and Data Visualization
Vectors
l A vector is an array of data consisting of either:
l A single row of values (sized at 1 × m), or
l A single column of values (sized at n × 1)
l To create a row vector in MATLAB, all values need to be
enclosed within the square brackets [], and separated by a
space (a comma is optional).
l To create a column vector in MATLAB, all values need to be
enclosed within the square brackets [], and separated by a
semicolon.
ELEE2319 Numerical Computation and Data Visualization
Vectors
ELEE2319 Numerical Computation and Data Visualization
Colon operator
Consider the following vector:
The generation of the vector x can be simplified using the
colon operator:
ELEE2319 Numerical Computation and Data Visualization
Sequential Row Vector
The elements of the vector x are: a, a+1, a+2, …, and the last
element is between b-1 and b
That is, the elements of the vector x are incremented by 1;
l a must be less or equal to b (a ≤ b)
l x = [a, a+1, a+2, …, c] where b-1 ≤ c ≤ b
ELEE2319 Numerical Computation and Data Visualization
Sequential Row Vector
The increment can be any value (positive or negative, integer
or real number). However, the following rules must be
followed:
x = [a : i : b]
The elements of the vector x are incremented by the value i
Case 1) the increment i is positive
l a must be less or equal than b (a ≤ b)
l x = [a, a+i, a+2*i, …, c] where b-i ≤ c ≤ b
Case 2) the increment i is negative
l a must be greater or equal than b (a ≥ b)
l x = [a, a+i, a+i*2, …, c] where b ≤ c ≤ b-i
ELEE2319 Numerical Computation and Data Visualization
Sequential Row Vector
ELEE2319 Numerical Computation and Data Visualization
Matrix
l A matrix is an array of data consisting of n rows and m
columns, where both n and m are greater than 1.
l To generate a matrix in MATLAB, a pair of square brackets []
need to enclose the data values in the command.
l Use a space to distribute values across columns, and a
l semicolon to distribute values across rows.
ELEE2319 Numerical Computation and Data Visualization
Matrix
ELEE2319 Numerical Computation and Data Visualization
The Transpose Operator (.’)
l The transpose operator is .’ (a period followed by
an apostrophe)
l A column vector can be generated from a row
vector by applying the transpose operator
l A row vector can be generated from a column
vector using the transpose operator
l The complex conjugate transpose operator is
obtained using just an apostrophe ’.
ELEE2319 Numerical Computation and Data Visualization
The Transpose Operator (.’)
ELEE2319 Numerical Computation and Data Visualization
Functions that Generate Vectors
The function linspace(a,b,n) generates a row vector with
elements having uniform linear spacing:
a: Start Value
b: End Value
n: number of elements in the vector
ELEE2319 Numerical Computation and Data Visualization
Examining the Elements of a Vector
l Each element of a vector or matrix has
an index associated with it.
l The index of an element is a positive
integer number x(n).
l To access an element of a matrix or
vector x, we specify the location of the
element within the array using the
command x(row, col), where row and
col are the row and column numbers
where the element is located.
ELEE2319 Numerical Computation and Data Visualization
Examining the Elements of a Vector
ELEE2319 Numerical Computation and Data Visualization
Examining the Elements of a Vector
l We can access more than one element
of a vector or matrix at once, using the
colon symbol
l x(row, col:col_n), where col and col_n
indicate the column interval.
l x(row:row_n, col), where row and
row_n indicate the row interval.
l x(row:row_n, col:col_n), where row,
row_n, col, and col_n indicate the row
and column interval.
l x(n1:n2), where n1:n2 indicate the
element interval of a vector.
ELEE2319 Numerical Computation and Data Visualization
Examining the Elements of a Vector
ELEE2319 Numerical Computation and Data Visualization
Examining the Elements of a Vector
l Once accessed, a set of elements of an
array can be modified with new values
as follows
l x(row, col)=new_value, where
new_value is the value for the element
of the array.
l x(row:row_n, col)=new_value
l x(row:row_n, col:col_n)=new_value
l x(n1:n2)=new_value
ELEE2319 Numerical Computation and Data Visualization
Examining the Elements of a Vector
ELEE2319 Numerical Computation and Data Visualization
Length and size of an array
The shape of an array x (matrix
or vector) can be accessed
through the command size(x)
The number of elements in a
vector x can be calculated using
the function length(x)
ELEE2319 Numerical Computation and Data Visualization
Types of Values (Data Types)
In MATLAB, there are many different data types. The most
commonly used in our course are:
• Numeric
• Integer and floating-point data
• Characters and Strings
• Text in character arrays and string arrays.
l Note that MATLAB does not require variables to be declared
to a data type before these are used, as in other programming
languages.
l Character and string manipulation is also of importance, to
allow interaction and communication between MATLAB and
the user.
ELEE2319 Numerical Computation and Data Visualization
Characters and Strings
l To assign an array of characters to a variable, such array
must be enclosed within single quotes ‘ ’.
l To assign a single string to a variable, such string must be
enclosed within double quotes “ ”.
l To assign an array of strings to a variable, each string must
be enclosed within double quotes “ ”, and the overall array
enclosed within square brackets [ ].
l It’s like building a “vector of strings”.
ELEE2319 Numerical Computation and Data Visualization
Characters and Strings
ELEE2319 Numerical Computation and Data Visualization
Characters and Strings
Both character arrays and string arrays are used for storage
of textual data, but are manipulated differently.
• A character array is a sequence of characters; typically used
to store short pieces of text as character vectors (one
character per column position in a vector).
• A string array is a container for larger pieces of text; these
provide a large set of sophisticated functions for working
with textual data.
For our purposes in this course, character arrays (using single
quotes ‘ ’) will suffice.
ELEE2319 Numerical Computation and Data Visualization
Characters and Strings
“Concatenate” means link or join together in series.
Here, A, B and C variables hold different pieces of text,
which are eventually concatenated in the fourth command as
if we were building a row vector.
Note that a space character is added between A and B.
Note under whos the sizing for each variable.
ELEE2319 Numerical Computation and Data Visualization
Characters and Strings
ELEE2319 Numerical Computation and Data Visualization
Displaying Data to User
The disp(var) MATLAB function is able to display:
• The numeric content of a variable var holding integers or
floating-point data.
• The textual content of a variable var holding characters or
string data. This function is much more useful in scripts, to
tell MATLAB when a message should be displayed to the
user through the Command Window.
ELEE2319 Numerical Computation and Data Visualization
Displaying Data to User
The disp(var) MATLAB function is able to display:
l The numeric content of a variable var holding integers
or floating-point data.
l The textual content of a variable var holding characters
or string data.
This function is much more useful in scripts, to tell MATLAB
when a message should be displayed to the user through the
Command Window.
ELEE2319 Numerical Computation and Data Visualization
Simultaneous Display of Textual and Numeric
Content
If using the disp MATLAB function to display both textual
and numeric data in a single line:
1. the numeric data NumericData must first be converted to a
textual data using the num2str(NumericData) function,
2. then concatenate all data together using [ ],
3. and finally display the concatenated data using the disp
function.
ELEE2319 Numerical Computation and Data Visualization
User Inputs in MATLAB
The var = input(prompt) MATLAB function is able to read
user input and store the input data into a variable var, while
displaying a prompt message to the user through the
Command Window.
ELEE2319 Numerical Computation and Data Visualization
User Inputs in MATLAB
ELEE2319 Numerical Computation and Data Visualization