+ All Categories
Home > Documents > 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5....

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

Date post: 18-Dec-2015
Category:
Upload: rhoda-atkinson
View: 225 times
Download: 1 times
Share this document with a friend
49
1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1
Transcript
Page 1: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

1. Definition2. Creating Cell Arrays3. Augmenting Cell Arrays4. Referencing Cell Arrays5. Special case6. Spreadsheets

1

Page 2: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

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

This chapter teaches how to operate more specifically with the cell array

2

Page 3: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Data type that holds information indexed in containers called cells

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

3

Simply It is an ARRAY of CELLS

Page 4: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Create a cell array by using the {} brackets Separate each element in the array with a

comma

4

Curly braces – not parentheses, not brackets!

Page 5: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Plot them using cellplot()!

5

FYI: It is just a representation. J is in the first box, o in the second, e in the third.

Page 6: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Adding on a new cell to the array Example, add on the string 'def'.

6

Page 7: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

{ } are not used to augment. They are strictly used to create new cell-arrays from scratch.

The curly braces indicate ‘def’ is added OUTSIDE of the cell-array C – i.e. C is a completely new cell-array!

7NOT what we wanted...

Page 8: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Augment the same as with normal arrays: use brackets

8

Page 9: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Can a 2D cell-array exist? Of course. Still use the ; operator to add a new row.

9

Page 10: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

There are 2 ways to reference a cell array, depending on the task:

1. Get the entire container as a whole. (Do not open the doors to see the content)

to move the container to extract/copy/replace the container to delete the container

2. Get the content inside the container. (Open the doors, and use the content)

To change its content To display/print its content

10

Page 11: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

1. Get the entire container as a whole. (Do not open the doors to see the content)

2. Get the content inside the container. (Open the doors, and use the content)

11

Parentheses ()

Curly Braces {}

Page 12: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Assume C starts with:

%option1: refer to & delete the 2nd container

12

Page 13: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Assume C starts with:

%option1: refer to & delete the 2nd container

%option2: refer to & delete the content of the 2nd container

13

Page 14: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

This is particularly important if you wish to use the values mathematically (equations, … )

For example:

Using the variable X mathematically creates issues...

14

The content (numerical value 2) is still inside the container, as indicated per the brackets.

Page 15: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

To use this variable X mathematically creates an error

After all, this is what MATLAB tries to execute:result = 2* + 5

15

?

Page 16: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Recall to use curly braces to access the CONTENT.

16

Use curly braces {}. Notice the lack of [] around the result.

Page 17: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

To conclude, also use curly braces to replace elements.

17

Initial data-base (name, age, weight)

Fix the name

Page 18: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

What if there is an array within a cell?

18

1) max?2) last score?

Page 19: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

What if there is an array within a cell?

"Max of the content of container 2". Use { }.

19

1) max?

Page 20: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

The following requires patient imagination skills, but it works and makes sense!

20

Simply: "Reference to the 3rd element in the CONTENT of the 2nd container"

Simply: "Reference to the last element in the CONTENT of the 2nd container"

Page 21: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Most would prefer storing the content in a intermediate variable, then referencing that variable.

21

Page 22: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Syntax: [NUM,TXT,RAW]=xlsread(FILE) You can select which sheet you want to read into

Matlab(the first sheet is the default) Reads data from the first worksheet in the Microsoft

Excel spreadsheet file named FILE NUM is a matrix with just the numerical values

found in the table. TXT is a cell array with the textual information

found in the table. RAW is a cell array with numbers and text

Syntax: xlswrite(FILE,ARRAY)

22

Example!

Page 23: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Syntax: RESULT = dlmread(FILENAME) The delimiter is inferred from the formatting of

the file. All data in the input file must be numeric. Fills empty delimited fields with zero dlmread( ) cannot handle text data file properly

23

Page 24: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Numbers have different precisions and formats delimiters (spaces, tabs, ;) separate columns

Space delimiter Comma delimiter

24

1 3.00 32

2 3.20 36

3 2.40 35

4 1.60 37

5 2.80 39

1, 3.00, 32

2, 3.20, 36

3, 2.40, 35

4, 1.60, 37

5, 2.80, 39

Page 25: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Syntax: RESULT= importdata(FILENAME) importdata( ) will separate the text columns and

the numeric columns into separate fields in a structure

The contents of the structure can be copied into numeric variables for analysis

25

Page 26: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Cells arrays are ‘cargo-ships’ Rectangular arrays allowing storage of different data-types into 1

variable.

Cell-arrays use all types of braces:Creating/hard-coding: Braces { }Referencing to content: Braces { }

Augmenting: Brackets [ ]

Referencing to container: Parentheses ()

For more useful information about Cell Arrays, please visit: http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-98.html

26

Most likely to be used often!

Page 27: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

1. Dialog Boxes: Overview2. Inputdlg()3. listdlg()4. msgbox()5. questdlg()6. menu()

27

Page 28: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Dialog boxes are “popup windows” that allows another means to communicate with the user. There are dialog boxes to collect input:inputdlg(), listdlg(), menu(), questdlg()

And there are dialog boxes to produce output:msgbox(), warndlg()

28

99% of the time, the command deals with cell arrays, either as arguments, as return-values, or both!

Page 29: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Timing wise: Modal (“waiting”) – they prevent the program from

continuing until the box is closed Non-modal (“non-waiting”) – the program

continues execution while the box remains open

Examples: Input boxes are generally modal – the program

waits for input before continuing. This is not changeable.

Output boxes default to non-modal (“non-waiting”) but can be made modal.

29

Page 30: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

inputdlg() – much like the input() function, except… It is presented in a GUI form There can be multiple prompts and multiple

values provided by the user.

ALL user-provided information is returned as strings in a cell array!

That’s why learning about cell-arrays is important!

30

Page 31: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Prompt the user for the coefficients to the quadratic equation, then solve for the roots. Requirements: Use a Graphical User Interface (GUI) to prompt

the values, specifically inputdlg()

31

Page 32: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Arguments A cell array of strings: This is the set of prompts for

the user. For example:prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'}

Return Values one (1) cell-array of strings: What the user provided

in the boxes

Full Exampleprompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};coeffs = inputdlg(prompts);

32

Cell-array, indicated by the curly braces.

Page 33: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

33

When collecting numbers, use str2double()to convert the cell-array (cargo-ship) into a numerical vector:

coeffs = str2double(coeffs);

Page 34: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

34

clearclcprompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};

flag=1;% No invalid inputs, or imaginary rootswhile (flag) % Collect the coeff’s from the user coeffs = inputdlg(prompts); coeffs = str2double(coeffs); a = coeffs(1); b = coeffs(2); c = coeffs(3); if (a==0 || (b^2 - 4*a*c) < 0) % No invalid inputs, or imaginary roots fprintf('invalid values\n');else flag=0;endend%calculate rootsroots(1) = (-b + sqrt(b*b-4*a*c))/(2*a);roots(2) = (-b - sqrt(b*b-4*a*c))/(2*a)

Page 35: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

35

clearclcprompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};

flag=1;% No invalid inputs, or imaginary rootswhile (flag) % Collect the coeff’s from the user coeffs = inputdlg(prompts); coeffs = str2double(coeffs); a = coeffs(1); b = coeffs(2); c = coeffs(3); if (a==0 || (b*b - 4*a*c) < 0) % No invalid inputs, or imaginary roots fprintf('invalid values\n');else flag=0;endend%calculate rootsroots(1) = (-b + sqrt(b*b-4*a*c))/(2*a);roots(2) = (-b - sqrt(b*b-4*a*c))/(2*a)

Extract the values from the vector: Must this be done, or should it be done for convenience?

Page 36: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

36

clearclcprompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};

flag=1;% No invalid inputs, or imaginary rootswhile (flag) % Collect the coeff’s from the user coeffs = inputdlg(prompts); coeffs = str2double(coeffs); if (coeffs(1)==0 || (coeffs(2)^2 - 4*coeffs(1)*coeffs(3)) < 0) % No invalid inputs, or imaginary roots fprintf('invalid values\n');else flag=0;endend%calculate rootsroots(1) =(-coeffs(2) + sqrt(coeffs(2)^2-4*coeffs(1)*coeffs(3)))/(2*coeffs(1));roots(2) =(-coeffs(2) - sqrt(coeffs(2)^2-4*coeffs(1)*coeffs(3)))/(2*coeffs(1))

Really..

Page 37: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

37

clearclcprompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};

flag=1;% No invalid inputs, or imaginary rootswhile (flag) % Collect the coeff’s from the user coeffs = inputdlg(prompts); coeffs = str2double(coeffs); a = coeffs(1); b = coeffs(2); c = coeffs(3); if (a==0 || (b*b - 4*a*c) < 0) % No invalid inputs, or imaginary roots fprintf('invalid values\n');else flag=0;endend%calculate rootsroots(1) = (-b + sqrt(b*b-4*a*c))/(2*a);roots(2) = (-b - sqrt(b*b-4*a*c))/(2*a)

Since the prompts do not change each time it loops, avoid having this line within the loop. Time & effort consuming for Matlab.

Page 38: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

38

Syntax: [SELECTION,OK]=listdlg('ListString',S) Creates a modal list-selection dialog box SELECTION is a vector of indices of the

selected strings ▪ length 1 in the single selection mode ▪ This will be [] when OK is 0

OK=1 if you push the OK button, OK=0 if you push the Cancel button or close

the figure.

Page 39: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Inputs are in parameter/value pairs (i.e. they go 2 by 2):

Parameter goes 1st, value of the parameter goes 2nd.

The actual string:

39

Page 40: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

myList is a CELL ARRAY of string: { }

Why cell arrays?The length of each selection varies

widely, and an regular-array would not be rectangular.

40

Page 41: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

41

The Select All button is gone.

Page 42: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Syntax: msgbox(Message, Title, Icon) Message is a string vector, string matrix or cell array. Could be modal or non-modal Title: specifies the title of the message box Icon: specifies which Icon to display in the message box.

▪ Icon is 'none', 'error', 'help', 'warn', or 'custom'. The default is 'none'.

42

Page 43: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Syntax: ButtonName = questdlg(Question, Title,Btn1,Btn2,Btn3,DEFAULT) creates a modal dialog box that automatically wraps the

cell array or string Up to 3 custom button names may be specified default = String that is the default button button = string on the button that was clicked If the dialog is closed without a valid selection, the return

value is empty.

43

Page 44: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

button = string on the button that was clicked

44

Page 45: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Syntax: button = menu('qstring','bt1','bt2',……,'btn')

qstring = question to ask user bt1 = String to show on Button #1 bt2 = String to show on Button #2

Can have as many options as desired. There is no default answer. Return value: Button number clicked (not the string)

45

Page 46: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

46

Page 47: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

47

Page 48: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

Dialog boxes are user friendly but time taking We saw:

inputdlg() listdlg() msgbox() questdlg() menu()

NEVER learn the syntax by heart. Practice it enough, then use the doc to remind yourself quickly!

48

Page 49: 1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.

If you want to do more than fundamental dialog boxes, study the MATLAB help documentation.

http://www.mathworks.com/help/techdoc/ref/f16-40727.html#f16-8026

49

F1 = Help


Recommended