Programming Environment

12
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

description

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. - PowerPoint PPT Presentation

Transcript of Programming Environment

Page 1: Programming Environment

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

Page 2: Programming Environment

2

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

Cell and Structure Topics

Cell Arrays Preallocating Cells Structures Structure Example: dir

Page 3: Programming Environment

3

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

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)

Page 4: Programming Environment

4

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

Cell Array Example 2 Row by 3 Column Array of Elements

Page 5: Programming Environment

5

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

Creating a Cell Array 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']};

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

Page 6: Programming Environment

6

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

» 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

Cell Example

To View Type or 1D Contents

To View Contents

Page 7: Programming Environment

7

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

» 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

Page 8: Programming Environment

8

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

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

Page 9: Programming Environment

9

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

» 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 Matlab Structures do not require definition like C

structures do Simply type the structure name and assigned field

Stored in Order of

Assignment

Page 10: Programming Environment

10

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

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

Page 11: Programming Environment

11

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

Dir Command Example» D=dir('*.m') Find All files with .m extensionD =

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') Find All files with .txt extensionD2 =

4x1 struct array with fields: Many files are found

name so only field names

date are shown

bytes

isdir

Page 12: Programming Environment

12

MATLAB Programming:U of M-Dearborn ECE DepartmentIntroduction to MATLAB and its Toolboxes

Cells and Structures

Accessing Structure DataD=dir('*.TXT'); % List of all text Files

FileNames = Result of Codeu1_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