Matlab - Tutor2 - Variables and Arrays
Matlab - Tutor2 - Variables and Arrays
The structural unit of data in any MATLAB program is the array. An array is a
collection of record values organized into rows and columns and known by a
single name. Arrays can be allocated as either vectors or matrices. The
term "vector" is generally used to define an array with only one dimension,
while the term "matrix" is usually used to determine an array with two or
more dimensions.
The numbers of the row state the size of an array, and the numbers of the
column in the array, with the numbers of the row, mentioned first. The total
number of items in the array will be the product of the number of rows and
the number of the column.
Individual items in an array are addressed by the array name followed by the
row and column of the particular item. If the array is a row or column
vectors, then only one subscripts are needed. For example, in the preceding
array a(2,1) is 3 and c(2) = 2.
MATLAB variables name must start with a letter, followed by any sequence of
letters, numbers, and the underscore (_) character. Only the first 63
characters are essential; if more than 63 are used, the remaining characters
will be ignored. If two variables are stated with names that only differ in the
64th character, MATLAB will treat them as the same variable.
Creating and Initializing Variables in MATLAB
MATLAB variables are automatically generated when they are initialized.
ADVERTISEMENT
var = expression;
where var is the name of the variables and expression is a scalar constant,
an array, or a combination of constants, other variables, and mathematical
operations (+, -, etc.). The value of the expression is computed using the
standard rules of mathematics, and the resulting values are saved in the
named variable. The semicolon at the last of the statement is optional. If the
semicolon is absent, the values assigned to var will be echoed in the
command window. If it is present, nothing will be shown in the Command
Window, even though the assignment has appeared.
var = 40i;
var2 = var/5;
x = 1; y = 2;
array = [1 2 3 4];
The first example generates a scalar variable of type double and saves the
imaginary number 40i in it.
The second example generates a scalar variable and saves the result of the
expression var/5 in it.
The third example shows that multiple assignment statements can be placed
on a single line, supported that they are divided by semicolons or commas.
The last example display that variables can also be initialized with arrays of
data. Such arrays are build up using brackets ([]) and semicolons. All of the
items of an array are listed in row order. In other words, the value in each
row are recorded from left to the right, with the top-most row first, and the
bottom-most row last. The single value within a row are separated by blank
spaces or commas, and the rows themselves are divided by semicolons or
newlines.
The following statements are all legal arrays that can be used to initialize a
variable:
The number of items in every row of an array must be the same, and the
number of items in every column must be the same. An expression such as
[1 2 3; 4 5];
is illegal because row 1 has three items, while row 2 has only two items.
Arrays can also be initialize using built-in MATLAB function. For example, the
function zero can be used to generate an all-zero array of any desired size.
There are a various form of the zeros function. If the function has an
individual scalar argument, it will develop a square array using the single
arguments as both the number of rows and the number of columns. If the
function has two scalar argument, the first arguments will be the number of
rows, and the second arguments will be the number of the columns. Since
the size function return two values including the number of row and column
in an array, it can be combined with the zero function to create an array of
zeros that is the same size of another array.
Some examples using the zeros function follow:
a = zeros(2);
b = zeros(2,3);
c = [1 2; 3 4];
d = zeros(size(c));
Similarly, the ones function can be used to generate array including all ones,
and the eye function can be used to generate arrays including identity
matrices, in which all on-diagonal items are one, while all off-diagonal items
are zero.
Following the tables containing a list of standard MATLAB functions useful for
initializing variables.
Functions Purpose
length(arr) Return the length of a vector, or the longest dimension of a 2-D array
size(arr) Return two values specifying the number of rows and columns in arr.
When this function is executed, MATLAB prints out the string 'Enter an input
value:,' and then waits for the client to respond. If the customer enters a
single number, it may just be typed in. If the customer enter an array, it
must be enclosed in brackets. In other case, whatever is typed will be stored
in variable my_val when the return key is entered. If only the return key is
entered, then an empty matrix will be generated and stored in the variable.
If the input function contains the character 's' as a second argument, then
the input data is returned to the customer as a character string. Thus, the
statement
o We can change the current working folder by using the Browse For
Folder button located above the current folder pane.
o After running the save command, the file saved with .mat extension
becomes visible in the Current Folder Pane.
o After saving the workspace, we can clear all the contents of the
workspace by using the clear command at the command line.
o Syntax of the save command:
o Before loading the file, ensure the parent folder is selected as the
current folder; otherwise, it will show error:
Workspace after saving the file, clearing the content of the workspace, and
before loading of the file:
Workspace after loading of the file, now we can use variables inside that file:
PlayNext
Mute
Duration 18:10
Loaded: 2.94%
Â
Fullscreen
ADVERTISEMENT
o When text is enclosed in double quotes (" "), its data type or class is
declared as String.
o Strings are also arrays, like all other MATLAB variables.
o Syntax:
o When the text of the String itself includes double quotes, then use
double quotes within the text, as:
o String array can also have multiple elements like number arrays.
ADVERTISEMENT