130 likes | 242 Views
Cells and Structures. Array Cells and Array Structures. Creating Cell Arrays. A = cell(2) creates a 2 by 2 array for cells Each cell can have a different content Note how content has be enclosed in curly brackets A(1,1) = {'ENDF 121'} A(1,2) = {'Fall 2005'} A(2,1) = { [1:7] }
E N D
Cells and Structures Array Cells and Array Structures
Creating Cell Arrays • A = cell(2) • creates a 2 by 2 array for cells • Each cell can have a different content • Note how content has be enclosed in curly brackets • A(1,1) = {'ENDF 121'} • A(1,2) = {'Fall 2005'} • A(2,1) = { [1:7] } • A(2,2) = { [22,15,22,23,24,25,21] }
Different way of accessing cells via 'content indexing' • B = cell(2,3) • creates a 2 by matrix whose cells are accessed by using { , } for the indices instead of ( , ) • B{1,1} = 'special matrices' • B{1,2} = zeros(3) • B{1,3} = ones(3) • B{2,1} = 'their inverse' • B{2,2} = 'does not exist' • B{2,3} = ones(3)
Creating cell arrays • It is not necessary to use the cell function first • but if variable exists already an error will occur • Can use clear to avoid this problem • cells can be multi dimensional, not just 2, i.e. m = cell( 2,3,4,2 ) • cells can be nested, i.e. a single cell element can reference another cell • Note difference between cell indexing • A(1,1) = {' This class '} • and content indexing • A{1,1} = 'This class' • and direct addressing • A(1,1) = 123; • in the last case the floating point number 123 is stored directly at the storage location associate with A(1,1). • the last case can not be mixed with the first two
Functions for working with cells • C = cell(m,n,p,…) • celldisp(C) • cellplot(C) • C = num2cell(A) • A numeric array – uses direct addressing • C array of cells – uses indirect addressing • iscell(C) returns 1 for true 0 for false • [X,Y,…] = deal(A,B,…) • same as X=A; Y=B; … • number of variables have to be the same on both sides • except for special case [X,Y,…] = deal(A)
Example for students in class • S{1,1} = 'Joe Smith' • S{1,2} = '123-45-6789' • S{1,3} = 'smithj@email.uc.edu' • S{1,4} = [67,78,45] ; • S{2,1} = 'Ann Schmidt' • S{2,2} = '234-45-1234' • S{2,3} = 'schmida@email.uc.edu' • S{2,4} = [78,95,67] ;
Difficulties with cells • Cells of array hold • name • social security number • e-mail address • scores for tests • First index refers to student number in class • When entering data need to remember all of this • Excel or Access or similar programs are a much better choice for this type of problem
Structures • Structures are similar to cells, except the cells can be referenced symbolically by a meaningful name (instead of an index) • student.name='John Smith' • student.SSN='123-45-6789' • student.email='smithj@email.uc.edu' • student.tests=[23,45,34] • when entering Matlab will repeat entry unless a semicolon is used
Array of structures • student(2).name='Mary Poppins' • student(2).SSN='456-12-2345' • student(2).email='poppins@email.uc.edu' • student(2).tests=[12,0,13] • when entering Matlab will respond each time with • student= • 1 * 2 struct array with fields: • name • SSN • email • tests • in this case better to use semicolon
Preallocation of structure • students=struct('name','Karl',tests,[12,34,67]) • students(2).name='Cindy' ; • students(2).tests=[13,24,34]; • Additional fields can be added later, that is dynamically • students(2).id = '123-23-3333'; • All existing array elements will now have the same entry available; initially set to empty. • All entries can be change by using the same notation • With the name notation each field can be retrieved
Copying structure arrays • studentcopy = students • all entries of structure array are copied • operation can be time consuming for very large arrays and also will require a lot of memory
Modifying a structure array • To remove a field in a structure array: • stdnt=rmfield( student,'email' ) • function returns a new structure array after copying all fields except the one for 'email' • Both structure arrays student and stdnt are accessible unless same name is used • student = rmfield( student, 'email' ) • To add a field can also use • stdnts=setfield( students, {2}, 'phone','985-5566') • entire structure array has to be copied with new field inserted. • array element 2, will have given phone number all other entries are empty • if structure array is two dimensional {2} needs to be replaced by reference to correct array element {i,j}
Accessing structures • getfield( student,{2},'name' ) • Matlab allows access to fields of a structure with a variable, i.e. dynamic access • xyz = 'name' • student(2).(xyz) • other programming languages typically do not have this feature and require that the field is known at compilation time, i.e. static access • Benefits of dynamic versus static access • probably slower in complied languages • probably faster in interpreted languages (i.e. interactive systems) like Matlab