LectureNotes8-24
LectureNotes8-24
Data structures are variables that store more than one value.
CELL ARRAYS
------------
One type of data structure, which MATLAB has but is not found in many
but, unlike the vectors and matrices we have used so far, elements in cell
In creating cell arrays, curly braces are used rather than square brackets
cellrowvec =
To create a column vector cell array, the values are instead separated by
semicolons:
{[ 23]}
{'a' }
{1x5 double}
{'hello' }
*************
cellmat =
{[ 23]} {'a' }
>> class(cellmat)
ans =
'cell'
mycellmat =
******
Using curly braces for the subscripts will reference the contents of a cell;
> cellrowvec{2}
ans =
'a'
class(ans)
'char'
sum(cellrowvec{3})
ans =
25
******
Row and column subscripts are used to refer to the contents of an element in
>> cellmat{1,1}
ans =
23
> cellrowvec(2)
ans=
{'a'}
class(ans)
'cell'
>> cellcolvec(2)
ans =
{'a'}
class(ans)
ans =
cell
Notice in the following the difference between what is displayed
cellmat =
{[ 23]} {'a' }
>> cellmat(2,1)
ans =
{1x5 double}
>> cellmat{2,1}
ans =
13579
>> cellmat{2,1}(4)
ans =
7
>> cellrowvec
cellrowvec =
>> cellrowvec(2) = []
cellrowvec =
cellrowvec{2}=[]
******
lengths:
names =
1×3 cell array
disp(length(names{i}))
end
The char function can convert from a cell array to a character matrix:
cnames =
'Sue '
'Cathy '
'Xavier'
>> size(cnames)
ans =
36
The function iscellstr will return logical true if a cell array is a cell
>> iscellstr(names)
ans =
>> iscellstr(cellcolvec)
ans =
%%%%
Subcells
cellarrsub ={
};
****
class(cellarrsub)
ans =
'cell'
size(cellarrsub)
ans=
4 2
*******
cellarrsub{3,2};
ans =
******
ans =
0.0200
******
y = eval([cellarrsub{1,1} '(x)']);
plot(x,y)
*****************
a{1,2}(3)=5 % or a{1,2}(1,3)=5 %
x=a{1,2};
plot(a{1,3}(x))
x=[-pi:0.01:pi];
plot(a{1,3}(x).^2)
*******************
STRUCTURES
-----------
Structures are data types that group together values that are logically
package
struct function:
package =
item_no: 123
cost: 19.9900
price: 39.9500
code: 'g'
ans =
struct
>> size(package)
ans =
1 1
%%%%
involves using the dot operator to refer to fields within the structure:
newpack =
item_no: 111
cost: 19.9900
price: 34.9500
code: 'g'
To print from a structure, the disp function will display either the entire
>> disp(package)
item_no: 123
cost: 19.9900
price: 39.9500
code: 'g'
>> disp(package.cost)
19.9900
However, using fprintf only individual fields can be printed; the entire
123 g
Newpack =
item_no: 111
cost: 19.9900
price: 34.9500
>> newpack
newpack =
item_no: 111
cost: 19.9000
price: 34.9500
code: 'g'
newpack =
item_no: 111
cost: 19.9000
price: 34.9500
%%%%
must use the dot operator to refer to the price and cost fields of the
input argument:
calcprof.m
--------------------------------------------------------
% software package
end
--------------------------------------------------------
>> calcprof(package)
ans =
19.9600
%%%
Alternatively, just the price and cost fields can be passed to the function
calcprof2.m
--------------------------------------------------
end
--------------------------------------------------
ans =
19.9600
*****
The fieldnames function will return the names of the fields that are
pack_fields =
'item_no'
'cost'
'price'
'code'
*******
Monday
Vectors of Structures
packages
packages(1)=struct('item_no',123,'cost',19.99,'price',39.95,'code','g');
packages(2)=struct('item_no',456,'cost', 5.99,'price',49.99,'code','l');
packages(3)=struct('item_no',587,'cost',11.11,'price',33.33,'code','w');
By starting with the last element, MATLAB would create a vector with
packages(3)=struct('item_no',587,'cost',11.11,'price',33.33,'code','w');
packages(1)=struct('item_no',123,'cost',19.99,'price',39.95,'code','g');
packages(2)=struct('item_no',456,'cost', 5.99,'price',49.99,'code','l');
To understand better:
If you do first:
packages(3) = struct('item_no',587,'cost',11.11,'price',33.33,'code','w');
then, if you type
packages(2)
ans =
item_no: []
cost: []
price: []
code: []
packages(1) = struct('item_no',123,'cost',19.99,'price',39.95,'code','g');
packages(2)
%%%%%%%%%
Typing the name of the variable will display only the size of the
>> packages
packages =
cost
price
code
To display one element in the vector (one structure), an index into the
>> packages(2)
ans =
item_no: 456
cost: 5.9900
price: 49.9900
code: 'l'
>> packages(1).code
ans =
disp(packages(i))
end
item_no: 123
cost: 19.9900
price: 39.9500
code: 'g'
item_no: 456
cost: 5.9900
price: 49.9900
code: 'l'
item_no: 587
cost: 11.1100
price: 33.3300
code: 'w'
For example, to print all of the costs, a for loop could be used:
for i=1:3
fprintf('%.3f\n',packages(i).cost)
end
19.990000
5.990000
11.110000
fprintf('%f\n',packages.cost)
19.990000
5.990000
11.110000
Using the dot operator to refer to all values of a field would result
packages.cost
ans =
19.9900
ans =
5.9900
ans =
11.1100
pc = [packages.cost]
pc =
To sum all three cost fields, the vector of cost fields is passed to the sum function:
sum([packages.cost])
ans =
37.0900
printpackages.m
----------------------------------------------------
function printpackages(packstruct)
no_packs = length(packstruct);
for i = 1:no_packs
packstruct(i).item_no, ...
packstruct(i).cost, ...
packstruct(i).price, ...
packstruct(i).code)
end
end
----------------------------------------------------
printpackages(packages)
%%%%
might store for the class. The vector structure variable, called student,
student
1 2 3 4
Once the data structure has been initialized, in MATLAB we could refer to
>> student
student =
name
id_no
quiz
and/or fields:
>> student(1)
ans =
id_no: 999
>> student(1).quiz
ans =
10.0000 9.5000 0 10.0000
>> student(1).quiz(2)
ans =
9.5000
>> student(3).name(1)
ans =
%%%%%%%%
For example, let's write a function that calculates and prints the
printAves.m
-----------------------------------------------------
function printAves(student)
% This function prints the average quiz grade
for i = 1:length(student)
ave = qsum/no_quizzes;
end
end
-----------------------------------------------------
>> printAves(student)
Name Average
******
Nested Structures
endpoint1 endpoint2
x y x y
2 4 1 6
lineseg=struct('endpoint1',struct('x',2,'y',4),'endpoint2',struct('x',1,'y',6))
Once the nested structure has been created, we can refer to different
parts of the variable lineseg. Just typing the name of the variable
>> lineseg
lineseg =
Typing the name of one of the nested structures will display the field names
>> lineseg.endpoint1
ans =
x: 2
y: 4
>> lineseg.endpoint1.x
ans =
P1=[lineseg.endpoint1.x lineseg.endpoint1.y];
P2=[lineseg.endpoint2.x lineseg.endpoint2.y];
P1 =
2 4
P2 =
1 6
d=sqrt((P2(1)-P1(1))^2+(P2(2)-P1(2))^2);
or
d=norm(P2-P1)
d=
2.2361
******
industrial use:
cyls
rad height
1 x 3 6 7
2 a 4 2 5
3 c 3 6 9
cyls(3)=struct('code','c','dimensions',struct('rad',3,'height',6),'weight',9);
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);
For these cylinders, one desired calculation may be the volume of each
is the height:
printcylvols.m
----------------------------------------------------------
function printcylvols(cyls)
% in a specialized structure
for i = 1:length(cyls)
vol = cylvol(cyls(i).dimensions);
end
end
end
----------------------------------------------------------
>> printcylvols(cyls)