47
Cell Arrays 1. Definition 2. Creating Cell Arrays 3. Referencing Cell Arrays 4. Augmenting Cell Arrays 5. Use of Cell Arrays 1

Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Embed Size (px)

Citation preview

Page 1: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Cell Arrays

1. Definition2. Creating Cell Arrays3. Referencing Cell Arrays4. Augmenting Cell Arrays5. Use of Cell Arrays

1

Page 2: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Data Types

• Recall the workspace frame in MATLAB. Currently, 3 types of data have been seen. They are called:

• This lecture teaches a new data type called cell arrays

2

char (strings)double (float) or int8, int16…logical

Page 3: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

1. Cell Arrays - Definition

• A data type that stores values of different types in indexed data containers called cells.

• A cell can hold any data type- string, integer, float, or even another cell array…

3

Simply It is an ARRAY of CELLS

Page 4: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Quick Vocabulary

Parentheses ( )

Brackets [ ]

Braces { }

4

Page 5: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

2. Creating Cell Arrays

• Cell arrays can be created by using the { } braces• Separate cells in a row with commas (or spaces); separate

rows with semi-colons.• Likes arrays, cell arrays need to be rectangular

5

Curly braces – not brackets!

Page 6: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

2. Creating Cell Arrays, cont.

• Visualize them using cellplot()!

6

J o e

Page 7: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

2. Creating Cell Arrays, cont.

• Cell arrays can be of higher dimensions

7

a 2 by 2 cell array

Page 8: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Question

Is this cell array, A = {1:4; 2:3}, rectangular?

8

Answer: Yes, it is rectangular. Its size is 2 by 1.

Page 9: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

3. Referencing Cell Arrays

• Like with arrays, row and column indices are used• Unlike arrays, there are 2 ways to reference a cell array,

depending on the task:

• Get the entire cell as a whole, use (). - to move the container- to extract/copy/replace the container- to delete the container

• Get the content inside the cell, use {}. - To change/empty its content- To display/print its content

9

Page 10: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

3. Referencing Cell Arrays

10

Parentheses

property of the cell is shown, not content

Page 11: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

3. Referencing Cell Arrays

11

Curly Braces

Content in the cell

A 1x1 Cell Array containing a 1x4 array of doubles

Page 12: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

3. Referencing Cell Arrays

12

Undefined function 'plus' for input arguments of type 'cell'.

>> x = cellmat(1,1);>> y = x+5;

Page 13: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

3. Referencing Cell Arrays

13

>> x = cellmat(1,1);

>> y = x+5; ✗

>> x = cellmat{1,1};

>> y = x+5; ✔y =

6 8 10 12

Page 14: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

3. Referencing Cell Arrays

14

Cell indexing: ( )

Content indexing: { }

Page 15: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

4. Cell Arrays – Augmenting

• Add the string 'def'.

15

??

Page 16: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

4. Cell Arrays – Augmenting

• { } are not used to augment. They are used to create new cell-arrays from scratch. - ‘def’ is added OUTSIDE of the cell-array C

16

NOT what we wanted...

Page 17: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

4. Cell Arrays – Augmenting

• Instead, augment using square brackets

17

Page 18: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

4. Cell Arrays – Augmenting

• Add a row? Of course!• Like with arrays, use ; to add a new row.

18

Page 19: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

5. More Operations…

• Given C = {1, 2, ‘Joe’, 3.4, ‘def’}– Delete the 5th cell

– Emptying contents of the 2nd cell

– Replace 3rd cell with the cell of content ‘sam’;

– Change content of 1st cell to the number 8

– Transpose, ‘, still works.19

C(5) = []

C{2} = []

C(3)= cellstr(‘sam’) OR C{3} = ‘sam’

C{1}= 8

Page 20: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

6. Why Cell Arrays?

• To store information of mixed data types• To store arrays of different sizes• To store strings of different length

Example:• Names and grades? • Daily temperature of 12 months. 28 days, 30 days or 31 days?• Names of different length?

20

Page 21: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Introduction to File I/OHigh-Level Functions

1. Data files2. “High level” File I/O3. dlmread()4. xlsread()

21

Page 22: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

1.1. File Applications

DatabasesLogs – attendance, events, historyJournals / DiariesAddress booksSensor dataDocuments

Almost every program uses files!

22

Shuttle ECO sensor

Automotive O2 sensor

Page 23: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

1.2 Data Files

• Data files can be different types and with different data organization

23

Page 24: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

1.2 Data Files, cont.

• Flagler Property Sales

24

Page 25: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

1.2 Data Files, cont.

• Grade

25

Page 26: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

1.3 File I/O

• There are basically three operation modes on files:– Read from: to grab/load data from a file and pass them to variables

(INPUT)– Write to: to store/save data to a file from the file beginning (OUTPUT) – Append to: to add/save data to the end of a file (OUTPUT)

• In general, 3 steps are always involved: – MATLAB opens the file;– MATLAB reads from, writes to, or appends to the file;– MATLAB close the file;

26

Page 27: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

2.1 High-level File I/O

• A file I/O function is considered HIGH-LEVEL if in ONE SINGLE COMMAND, it 1. Opens the file2. Reads from the file, writes to the file, or appends to the file3. Closes the file

• “Low-level” I/O functions are to be introduced next week. Those will require more lines of code!

27

Page 28: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

2.2 Can High-level be used?

• To decide if using a high level function is possible, evaluate the following:

1. What type of file is it? (Excel, text, jpg..)

2. Recognize the data types (all numerical? all strings? Or combination of both)

3. Find the organization overall (data in rows, or data in columns, data placed all over the place…)

4. Recognize the delimiters: What makes it obvious it is a new column? What makes it obvious it is a new row?

• space, tabs, comma, new lines, dash, colon, /, any specific symbol!

28

Page 29: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

2.2 Can High-level Be Used?

29

Page 30: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

3.1 ASCII Delimited Files

• “Neatly Organized”

1. Rows and Columns are “identifiable”2. Always the same patterns per line3. There is no mixes of delimiters

(commas, spaces, tabs, dash.. )

30

A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams

Page 31: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Questions

• Can these files be read using dlmread()?

31

Page 32: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

The data files (.txt, .xls, .xlsx, .m) should all be in the same directory for function calls to work!

32

Page 33: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

3.2 dlmread()

Syntax M = dlmread(filename);

Reads numeric data from the ASCII delimited file filename, and returns the data in output matrix M. The input filename is a string enclosed in single quotes. The delimiter is inferred from the formatting of the file.

M = dlmread(filename, delimiter);

Reads numeric data from the ASCII delimited file filename using the delimiter delimiter such as '-', ':'and etc. (Use '\t' to specify a tab.)

• >>doc dlmread <enter> open further possible syntaxes33

Page 34: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

3.3 Specific Delimiters, cont.• Delimiter: other than the defaults

34

Specify the delimiter as the 2nd argument.

Page 35: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

3.3 Using Delimiters, cont.

• BAD… Mix of delimiters.

35

Two delimiters (spaces and colons) in the file. MATLAB is lost.

>> “low-level” functions are necessary for this

Page 36: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Example1: airfoils

• The NACA airfoils are airfoils shapes for aircraft wings. Load the data (x and y coordinates) for this model of NACA airfoil and plot it.

36

www.angelfire.com

Page 37: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Example1: airfoils

clcclear

%load the numerical data file

%slice the array to extract x and y coordinates

% plot the airfoil and format the plotplot(x,y)title('Naca2410') %add plot titleaxis equal %make axis equal in scalegrid on %put the grid on

37

array = dlmread('naca2410.dat');

x = array(:,1);y = array(:,2);

Remember those quotes and the extension.

Page 38: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

38

without files…clcclear

%prompt how many sets of x-y coordinatessets = input('How many sets (>5)? ');while sets<=5 || mod(sets,1)~=0 %decimals sets = input('ERROR: How many sets (>5)? ');end %prompt user for x/y coordinatesfor k = 1:sets %ask for x fprintf('x%d: ',k); x(k) = input(''); %ask for y fprintf('y%d: ',k); y(k) = input(''); fprintf('\n');end % plot the airfoil and format the plotplot(x,y)title('Naca2410') %add plot titleaxis equal %make axis equal in scalegrid on %put the grid on

Page 39: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

2. xlsread()

39

Page 40: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

2. xlsread(), Syntax

Syntax[num,txt,raw] = xlsread(filename) reads data from the first worksheet; [num,txt,raw] = xlsread(filename, sheet) reads the specified worksheet.

Numeric data in the spreadsheet are returned to array num. Optional: Textual information in the spreadsheet is returned to cell array txt, and the unprocessed data (numbers and text) to cell array raw. filename is a string. If hardcoded, it must be in single quotes.

40

Page 41: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Example: xlsread()

• Load the grade data from a spreadsheet and find the name of the student who has the highest grade.

41

>>[age_grade, text, raw] = xlsread('grades.xlsx’)

Page 42: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

42

age_grade = 19 78 22 83 98 99 21 56 23 89 19 51

myText= 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy’ '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' ''

>> [age_grade, myText, raw] = xlsread('grades.xlsx')

6X2 double

7X3 cell

7X3 cell

raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51]

Example: xlsread()

Page 43: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

43

myText = 19 78 22 83 98 99 21 56 23 89 19 51

age_grade = 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy’ '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' ''

raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51]

Example: xlsread()

• variable names don’t matter to MATLAB. It respects its order.>> [myText, age_grade, raw] = xlsread('grades.xlsx‘)

Page 44: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Example: xlsread()

• Now find student who has the highest grade.

44

age_grade = 19 78 22 83 98 99 21 56 23 89 19 51

text= 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy’ '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' ''

raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51]

Page 45: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

Example: xlsread()

• Find the student who has the highest grade%load data from file [age_grade, text] = xlsread('grades.xlsx' );

%find the position of the maximum grade [trash row]= max(age_grade(:,2) );

%find whose name is associated with that position name = text{row+1,1} ; %+1 due to headers fprintf('%s got the highest grade\n' , name)

• NOTE: raw would be useless here. We simply did not collect it…

45

Page 46: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

xlsread(), cont.

• OMIT collecting the 2nd and 3rd return variables if only numerical values are of interest!age_grade = xlsread(‘grades.xlsx’);

• If a project needs all the data together, collect the 1st and 2nd return values into a dummy variable.[trash trash data] = xlsread(‘grades.xlsx’);

or since MATLAB R2009b, use tilde (~):[~, ~, data] = xlsread(‘grades.xlsx’);

• If there happens to be ‘holes’ in the spreadsheet, MATLAB fills it with a NaN value (not a number). The function isnan() can help determine where those ‘holes’ are.

46

Page 47: Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

47

Testing habits

• Though the file may contain 1,000,000 lines, and ONE command does the job, it is best to test the analysis with a smaller file:– create a copy of the original file, where only 5-10 lines are present.