Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cells and Structures Array Cells and Array Structures.

Similar presentations


Presentation on theme: "Cells and Structures Array Cells and Array Structures."— Presentation transcript:

1 Cells and Structures Array Cells and Array Structures

2 Character Strings planets=['Mercury','Venus','Earth'] Matlab returns one concatenated character string MercuryVenusEarth planets(1) is therefore only the first character, that is 'M' Planets={'Mercury','Venus','Earth'} Matlab now returns 3 separate character strings 'Mercury' 'Venus' 'Earth' Planets(1) is now 'Mercury' Planets{1} is then Mercury without the quotes

3 Array of one dimensional cells ['Mercury' ; 'Venus' ; 'Earth'] would avoid the catenation, but it is illegal since the three strings are not of equal length. (It is also inefficient.) {'Mercury','Venus','Earth'} is a one dimensional cell array Content of each cell can be of different type p={'Mars', 3.7, '2005', 2005} but if they are different accessing the cells becomes difficult Note p{3} and p{4} are different – see 5*p{3} and 5*p{4} Use structures instead – see later

4 Creating Cell Arrays A = cell(2) creates a 2 by 2 array for cells Each cell can have a different content Note how content of a cell 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] }

5 Different way of accessing cells via 'content indexing' B = cell(2,3) creates a 2 by 3 array 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)

6 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, 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

7 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)

8 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] ;

9 Difficulties with cells Cells of previous example hold information in following order (second index) 1. name 2. social security number 3. e-mail address 4. 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 Cells are useful for annotating graphs, identifying data, etc.


Download ppt "Cells and Structures Array Cells and Array Structures."

Similar presentations


Ads by Google