Computer Programming
with MATLAB
CONTACT SESSION 14
Dr.Pankaj Wankhede
BITS Pilani
Pilani | Dubai | Goa | Hyderabad WILP – BITS Pilani
Computer Programming
with MATLAB
Cell Array
Creating a row vector cell array
Creating a column vector cell array
Cell array Matrix
Display all the contents of Cell Array
Structures
Creating a structure
Add a new field to structure
Remove a field from structure
Creating Structure of more than one record
Examples
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Cell Array
Kind of data structure that stores values of different types.
A cell array can be vectors or matrices; the different values
are stored in the elements of the array.
Creating a row vector cell array:
⚫ Creating a column vector cell
array:
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Cell Array
⚫ Creating a Matrix (Cell Array Matrix):
⚫ to and Displaying Cell A rr ay Elements and Referring
Attributes :
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Cell Array
When an element of a cell array is itself a data structure, only
the type of the element is displayed when the cell array
contents are shown.
For example, in cellmat there is an element 1 x 5 double.
To show the content of this array
⚫ Since this element is a vector, parentheses are
used to refer to its elements.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Display all the contents of Cell Array
⚫ The celldisp function displays all elements of the cell
array
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Structures
Structures are data structures that group together values that
are logically related but are not same thing and not
necessarily the same type.
The different values stored in separate fields of structure.
An advantage of structures is that the fields are named, which
helps to make it clear what the values are that are stored in
the structure.
Creating Structure:
Creating structure variables can be accomplished by using
the struct function.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Creating Structures
⚫ Consider a store wants to save information about the
software packages that it sells. For every software they will
store:
■■ The item number
■■ The cost to the store
■■ The price to the customer
■■ A code indicating the type of software
The name of the structure variable is package;
it has four fields called item_no, cost, price, and
code. BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Creating Structures
⚫ Consider a store wants to save information about the
software packages that it sells. For every software they will
store:
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Add a new Field to Structure
This is done using dot
operator:
>>package.name = ‘JAVA’
package =
item_no: 123
cost: 19.9900
price: 39.9500
code: 'g'
name: 'JAVA '
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Remove a Field from Structure
This is done using rmfield:
>>rmfield(package,‘code’)
package =
item_no: 123
cost: 19.9900
price: 39.9500
name: 'JAVA '
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Creating Structure of more than one record
⚫ In many applications, including database applications,
would be stored in a vector of
information normally
structures.
⚫ For Example
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Creating Structure of more than one record
The variable packages is now a vector of structures, so each
element in the vector is a structure.
To display one element in the vector (one structure), an index
into the vector would be specified.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Creating Structure of more than one record
⚫ To refer a particular field in on element i.e.
⚫ For display all the elements of a particular field:
⚫ Store all values of particular field to a variable:
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example
⚫ Write MATLAB program that receives the entire vector of
structures as input argument, and print all of it in a nice table
format.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example
⚫ Write the given structure in MATLAB. Also compute the
volume (pi*r^2*h) of each cylinder and display it.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example
cyls(1) = struct('code','x','dimensions',struct('rad',3,'height',6),'weight',7);
cyls(2) = struct('code','a','dimensions',struct('rad',4,'height',2),'weight',5);
cyls(3) = struct('code','c','dimensions',struct('rad',3,'height',6),'weight',9);
for i=1:length(cyls)
x = cyls(i).dimensions;
volume = pi*(x.rad)*(x.rad)*x.height;
fprintf('Cylinder with code %s having volume %.2f\n', cyls(i).code,volume)
end
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956