140 likes | 174 Views
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. Cell and Structure Topics. Cell Arrays Preallocating Cells Structures Structure Example: dir. Cell Arrays.
E N D
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
Cell and Structure Topics • Cell Arrays • Preallocating Cells • Structures • Structure Example: dir
Cell Arrays • Cells are containers that hold other Matlab Arrays • One Cell Array may contain • A Real Matrix • An Array of Strings • Vector of complex values • A Cell of other Matlab Arrays • Cells may be High-Dimensional(more than just 2D)
Cell Array Example • 2 Row by 3 Column Array of Elements
Cell Indexing Creating a Cell Array » MyCell(1,1) = {[1 2 3; 2 3 4]}; » MyCell(1,2) = {1:0.2:2}; » MyCell(2,1) = {4+7i}; » MyCell(2,2) = {['The '; 'LAST';'cell']}; • Content Indexing » MyCell{1,1} = [1 2 3; 2 3 4]; » MyCell{1,2} = 1:0.2:2; » MyCell{2,1} = 4+7i; » MyCell{2,2} = ['The '; 'LAST';'cell'];
To View Type or 1D Contents To View Contents Cell Example » MyCell ={ [1 2 3; 2 3 4], 1:0.2:2; 4+7i, ['The '; 'LAST';'cell']} Cell = [2x3 double] [1x6 double] [4.0000+ 7.0000i] [3x4 char ] » MyCell(1,1) ans = [2x3 double] » MyCell{1,2} ans = 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000
Assigning a cell displays properties of entire cell Preallocating Cells • To preallocate a Cell Array, use cell command » B=cell(3,2) B = [] [] [] [] [] [] » B(2,1)={1:0.01:2} B = [] [] [1x101 double] [] [] []
Structures • 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 (1D, 2D, 3D, …) • A single structure is a 1x1 Structure Array
Stored in Order of Assignment Building Structures • 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 = name: 'Ted' type: 'aquatic’ age: 10 color: {1x2 cell}
Not same size as 'aquatic' Any Cell Displays size and field names Adding Structures to Array » turtle(2).name ='OneShot'; » turtle(2).type ='land'; » turtle(2).age = 1; » turtle(2).color ={'Sandy brown'} turtle = 1x2 struct array with fields: name type age color
Dir Command Example » D=dir('*.m') Find All files with .m extension D = name: 'comparescores.m’ Only one is found so date: '15-Jul-1998 11:43:42’ all fields are shown bytes: 4677 isdir: 0 » D2=dir('*.txt') FindAll files with .txt extension D2 = 4x1 struct array with fields: Many files are found name so only field names date are shown bytes isdir
Accessing Structure Data D=dir('*.TXT'); % List of all text Files maxFiles = size(D,1); % Number of retrieved text files for I=1:maxFiles, % For all files in the directory FileNames(I,1:size(D(I).name,2))= D(I).name; end FileNames is created and updated as Necessary FileNames = Result of Code u1_040898_1601.txt u1_040898_1602.txt u1_041398_1428.txt u1_041398_1429.txt