Download Chapter 8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Cell membrane wikipedia , lookup

Cell encapsulation wikipedia , lookup

Biochemical switches in the cell cycle wikipedia , lookup

Cytosol wikipedia , lookup

Endomembrane system wikipedia , lookup

Extracellular matrix wikipedia , lookup

Amitosis wikipedia , lookup

JADE1 wikipedia , lookup

Cellular differentiation wikipedia , lookup

Programmed cell death wikipedia , lookup

Cell cycle wikipedia , lookup

Cell culture wikipedia , lookup

Cell wall wikipedia , lookup

Cell growth wikipedia , lookup

Mitosis wikipedia , lookup

Organ-on-a-chip wikipedia , lookup

Cytokinesis wikipedia , lookup

List of types of proteins wikipedia , lookup

Transcript
Data Structures: Cell Arrays and Structures
Chapter 8
Copyright © 2013 Elsevier Inc. All rights reserved
1
CellArrays
— A cell array is a type of data structure that can store
different types of values in its elements
— A cell array could be a vector (row or column) or a
matrix
— It is an array, so indices are used to refer to the
elements
— One great application of cell arrays: storing strings of
different lengths
Copyright © 2013 Elsevier Inc. All rights reserved
2
CreatingCellArrays
— The syntax used to create a cell array is curly braces { }
instead of [ ]
— The direct method is to put values in the row(s)
separated by commas or spaces, and to separate the
rows with semicolons (so, same as other arrays) – the
difference is using { } instead of [ ]
— The cell function can also be used to preallocate by
passing the dimensions of the cell array, e.g.
cell(4,2)
Copyright © 2013 Elsevier Inc. All rights reserved
3
ReferringtoCellArrayElements
— The elements in cell arrays are cells
— There are two methods of referring to parts of cell arrays:
— you can refer to the cells; this is called cell indexing and parentheses
are used
— you can refer to the contents of the cells; this is called content
indexing and curly braces are used
— For example:
>> ca = {2:4, 'hello'};
>> ca(1)
ans =
[1x3 double]
>> ca{1}
ans =
2 3 4
Copyright © 2013 Elsevier Inc. All rights reserved
4
CellArrayFunctions
— the celldisp function displays the contents of all
elements of a cell array
— cellplot puts a graphical display in a Figure Window
(but it just shows cells, not their contents)
— to convert from a character matrix to a cell array of
strings: cellstr
— iscellstr will return logical true if a cell array is a cell
array of all strings
Copyright © 2013 Elsevier Inc. All rights reserved
5
StructureVariables
— Structures store values of different types, in fields
— Fields are given names; they are referred to as
structurename.fieldname using the dot operator
— Structure variables can be initialized using the struct
function, which takes pairs of arguments (field name
as a string followed by the value for that field)
— To print, disp will display all fields; fprintf can only
print individual fields
Copyright © 2013 Elsevier Inc. All rights reserved
6
StructExample
>> subjvar = struct('SubjNo’,123,'Height’,62.5);
>> subjvar.Height
ans =
62.5000
>> disp(subjvar)
SubjNo: 123
Height: 62.5000
>> fprintf('The subject # is %d\n',...
subjvar.SubjNo)
The subject # is 123
Copyright © 2013 Elsevier Inc. All rights reserved
7
CellArraysvs.Structs
— Cell arrays are arrays, so they are indexed
— That means that you can loop though the elements in a
cell array – or have MATLAB do that for you by using
vectorized code
— Structs are not indexed, so you can not loop
— However, the field names are mnemonic so it is more
clear what is being stored in a struct
— For example:
variable{1} vs. variable.weight: which is more mnemonic?
Copyright © 2013 Elsevier Inc. All rights reserved
8
StructureFunctions
— the function rmfield removes a field but doesn’t alter
the variable
— the function isstruct will return logical 1 if the
argument is a structure variable
— the function isfield receives a structure variable and a
string and will return logical 1 if the string is the name
of a field within the structure
— the fieldnames function receives a structure variable
and returns the names of all of its fields as a cell array
Copyright © 2013 Elsevier Inc. All rights reserved
9
VectorofStructures
— A database of information can be stored in MATLAB in a vector
of stuctures; a vector in which every element is a structure
— For example, for a medical experiment information on subjects
might include a subject #, the person’s height, and the person’s
weight
— Every structure would store 3 fields: the subject #, height, and
weight
— The structures would be stored together in one vector so that you
could loop through them to perform the same operation on
every subject – or vectorize the code
Copyright © 2013 Elsevier Inc. All rights reserved
10
Example
>> subjvar(2) = struct('SubjNo', 123, 'Height',...
62.5, 'Weight', 133.3);
>> subjvar(1) = struct('SubjNo', 345, 'Height',...
77.7, 'Weight', 202.5);
— This creates a vector of 2 structures
— The second is created first to preallocate to 2 elements
— A set of fields can be created, e.g.
>> [subjvar.Weight]
ans =
202.5000 133.3000
Copyright © 2013 Elsevier Inc. All rights reserved
11
ExampleProblem
packages
1
2
3
item_no
123
456
587
cost
19.99
5.99
11.11
price
39.95
49.99
33.33
code
‘g’
‘l’
‘w’
We will write general statements (using the programming
method) that will print the item number and code fields of
each structure in a nice format
for i = 1:length(packages)
fprintf('Item %d has a code of %c\n', ...
packages(i).item_no, packages(i).code)
end
Copyright © 2013 Elsevier Inc. All rights reserved
12
NestedStructures
— A nested structure is a structure in which at least one
field is another structure
— To refer to the “inner” structure, the dot operator
would have to be used twice
— e.g. structurename.innerstruct.fieldname
— To create a nested structure, calls to the struct
function can be nested
Copyright © 2013 Elsevier Inc. All rights reserved
13
Nestedstructexample
— The following creates a structure for a contact that
includes the person’s name and phone extension. The
name is a struct itself that separately stores the first
and last name.
— The calls to struct are nested
>> contactinfo = struct(...
'cname', struct('last', 'Smith', 'first', 'Abe'),...
'phoneExt', '3456');
>> contactinfo.cname.last
ans =
Smith
Copyright © 2013 Elsevier Inc. All rights reserved
14
CommonPitfalls
— Confusing the use of parentheses (cell indexing)
versus curly braces (content indexing) for a cell array
— Forgetting to index into a vector using parentheses or
to index into a cell array using parentheses or curly
braces or referring to a field of a structure using the
dot operator
— Thinking that you can index into a structure
Copyright © 2013 Elsevier Inc. All rights reserved
15
ProgrammingStyleGuidelines
— Use arrays when values are the same type and represent in
some sense the same thing.
— Use cell arrays or structures when the values are logically
related but not the same type nor the same thing.
— Use cell arrays rather than character matrices when storing
strings of different lengths
— Use cell arrays rather than structures when it is desired to
loop through the values or to vectorize the code.
— Use structures rather than cell arrays when it is desired to
use names for the different values rather than indices.
Copyright © 2013 Elsevier Inc. All rights reserved
16