Introduction to Matlab Programming Environment Cells and Structures

Introduction to Matlab: Programming Environment Cells and Structures S. Awad, Ph. D. M. Corless, M. S. E. E. E. C. E. Department University of Michigan-Dearborn

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Cell and Structure Topics Cell Arrays n Preallocating Cells n Structure Example: dir n 2

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Cell Arrays n Cells are containers that hold other Matlab Arrays n One Cell Array may contain u. A Real Matrix u An Array of Strings u Vector of complex values u A Cell of other Matlab Arrays n Cells may be High-Dimensional (more than just 2 D) 3

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Cell Array Example n 2 Row by 3 Column Array of Elements 4

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Creating a Cell Array n Cell Indexing » » n My. Cell(1, 1) My. Cell(1, 2) My. Cell(2, 1) My. Cell(2, 2) = = {[1 2 3; 2 3 4]}; {1: 0. 2: 2}; {4+7 i}; {['The '; 'LAST'; 'cell']}; Content Indexing » » My. Cell{1, 1} My. Cell{1, 2} My. Cell{2, 1} My. Cell{2, 2} = = [1 2 3; 2 3 4]; 1: 0. 2: 2; 4+7 i; ['The '; 'LAST'; 'cell']; 5

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Cell Example » My. Cell ={ [1 2 3; 2 3 4], 1: 0. 2: 2; 4+7 i, ['The '; 'LAST'; 'cell']} Cell = [2 x 3 double] [4. 0000+ 7. 0000 i] » My. Cell(1, 1) [1 x 6 double] [3 x 4 char ] To View Type or 1 D Contents ans = [2 x 3 double] » My. Cell{1, 2} ans = 1. 0000 1. 2000 To View Contents 1. 4000 1. 6000 1. 8000 2. 0000 6

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Preallocating Cells n To preallocate a Cell Array, use cell command » B=cell(3, 2) B = [] [] [] » B(2, 1)={1: 0. 01: 2} B = [] [1 x 101 double] [] [] Assigning a cell displays properties of entire cell 7

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Structures n n Structures are Matlab arrays with names “Data Containers” called fields A structure may hold any type of Matlab data Structure Arrays are multidimensional arrays of structures (1 D, 2 D, 3 D, …) A single structure is a 1 x 1 Structure Array 8

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Building Structures n n Matlab Structures do not require definition like C structures do Simply type the structure name and assigned field » turtle. name = 'Ted'; » turtle. type = 'aquatic'; » turtle. age = 10; » turtle. color = {'Brown Shell', 'Red Lines'} turtle = Stored in Order of name: 'Ted' Assignment type: 'aquatic’ age: 10 color: {1 x 2 cell} 9

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Adding Structures to Array » » turtle(2). name turtle(2). type turtle(2). age turtle(2). color ='One. Shot'; Not same size as 'aquatic' ='land'; = 1; ={'Sandy brown'} Any Cell turtle = 1 x 2 struct array with fields: name type Displays size and field names age color 10

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Dir Command Example » D=dir('*. m') Find All files with. m extension D = name: 'comparescores. m’ date: '15 -Jul-1998 11: 43: 42’ bytes: 4677 isdir: 0 » D 2=dir('*. txt') Only one is found so all fields are shown Find All files with. txt extension D 2 = 4 x 1 struct array with fields: name date bytes isdir Many files are found so only field names are shown 11

MATLAB Programming: Cells and Structures Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Accessing Structure Data D=dir('*. TXT'); % List of all text Files max. Files = size(D, 1); % Number of retrieved text files for I=1: max. Files, directory % For all files in the File. Names(I, 1: size(D(I). name, 2))= D(I). name; end File. Names is created and updated as Necessary File. Names = u 1_040898_1601. txt u 1_040898_1602. txt u 1_041398_1428. txt u 1_041398_1429. txt Result of Code 12
- Slides: 12