Download presentation

Presentation is loading. Please wait.

Published byNeil Cortner Modified about 1 year ago

1

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

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

4
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Cells and Structures 3 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 2D)

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

6
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Cells and Structures 5 Creating a Cell Array n Cell 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']}; n 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'];

7
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Cells and Structures 6 » MyCell ={ [1 2 3; 2 3 4], 1:0.2:2; 4+7i, ['The '; 'LAST';'cell']} Cell = [2x3 double] [1x6 double] [ i] [3x4 char ] » MyCell(1,1) ans = [2x3 double] » MyCell{1,2} ans = Cell Example To View Type or 1D Contents To View Contents

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

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

10
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Cells and Structures 9 » 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} Building Structures n Matlab Structures do not require definition like C structures do n Simply type the structure name and assigned field Stored in Order of Assignment

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

12
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Cells and Structures 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 :43:42’ all fields are shown bytes: 4677 isdir: 0 » D2=dir('*.txt') Find All files with.txt extension D2 = 4x1 struct array with fields: Many files are found name so only field names date are shown bytes isdir

13
MATLAB Programming: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Cells and Structures 12 Accessing Structure Data D=dir('*.TXT');% List of all text Files FileNames = Result of Code u1_040898_1601.txt u1_040898_1602.txt u1_041398_1428.txt u1_041398_1429.txt 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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google