1 / 12

Programming Environment

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.

tarala
Download Presentation

Programming Environment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Cell and Structure Topics • Cell Arrays • Preallocating Cells • Structures • Structure Example: dir

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

  4. Cell Array Example • 2 Row by 3 Column Array of Elements

  5. 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'];

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

  7. 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] [] [] []

  8. 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

  9. 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}

  10. 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

  11. 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

  12. 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

More Related