Cell Arrays Lecture 2/8/2010 1. Data Types (Review) 2. General Concept 3. Using Cell-Arrays 1....

Preview:

Citation preview

Cell ArraysLecture 2/8/2010

1. Data Types (Review)

2. General Concept

3. Using Cell-Arrays1. Syntax/Symbols

2. Dialog Boxes

1

1. Data Types Recall your workspace. We have currently seen 4 types of

data. They are called:

Chapter 7 teaches how to operate more specifically with the cell array

2

2. General Concepts

Still an array Still has to be rectangular BUT:

Each cell is more of a CONTAINER rather than one single element

3

This is just a nice picture. It still has to be rectangular. :o) But the container itself can be empty, have different data types, different content inside…

General Concepts, cont.

“Cells” are a structure used by MATLAB. They…

• can contain any data type: string, integer, float, another cell

• may be mixed within an array (unlike strings, integers, and floats)

• are used with many library functions to provide a “use anything” format

• use a slightly different syntax for creating, deleting, and referencing

“Cell Arrays” are exactly that – arrays of “cells” 4

Quick Vocabulary

Parentheses ( )

Brackets [ ]

Braces { }

5

3. Using Cell Arrays

Creating cell arrays:

>> C = {1, 2, 'abc', 3.4}

C =

[1] [2] 'abc' [3.4000]

Note the braces – not parentheses, not brackets!

Note how numbers in cells are displayed with brackets

But strings are not!6

Cell Arrays, cont.

Building cell arrays (i.e. concatenating)– let’s try braces:

C = {C, 'def'}

C =

{1x4 cell} 'def'

Wrong technique! Building cell-arrays uses a different syntax!

HUH?! What’s that?

Notice the curly braces? That indicates that we added a string OUTSIDE of the cell-array – we put the cell array INTO another cell-array! NOT what we wanted!!

7

Cell Arrays, cont.To build a cell array:

>> C = {1, 2, 'abc'}

C =

[1] [2] 'abc'

>> C = [C, 3.45]

C =

[1] [2] 'abc' [3.4500]

1st: Create the cell array – it can even be an empty cell array, if you wish

2nd: Build as you would with normal arrays: use brackets

8

Cell Arrays, cont.Referencing cell arrays:There are two ways to reference a cell array.

One way gets the entire cell. Think of it like getting an M&M: the nice gooey inside is wrapped in a hard candy…

>> x = C(1) %like old times, regular parentheses

x =

[2]The gooey inside (the “2”) is wrapped in brackets. Is this a problem? Let’s try to use it…

9

Cell Arrays, cont.>> y = x + 5??? Undefined function or method 'plus' for input arguments of type 'cell'.

MATLAB says that we’re trying to add to a “cell”

Oops – I guess the candy covering was too hard

We want to add to the contents of the cell – we need to peel off that coating

To do that, use curly braces – get just the contents, not the coating (“wrapper”)

10

Cell Arrays, cont.

>> x = C{1} %access INSIDE the cell

x =

2

>> y = x + 5

y =

7

So using braces {} is the second way to reference a cell array. However, this time it accesses the CONTENT.

Normally, you will want to use the braces so that you get the value inside the “wrapper”.

11

Cell Arrays, cont.Use braces also to replace elements – ‘cause you’re

leaving the wrapper but changing the gooey center:

>> C{1} = 5 %change 1st to 5

C =

[5] 'abc' [3.4500]

>> C{2} = 'def' %change 2nd to %string ‘def’

C =

[5] 'def' [3.4500]

>> C %pre-built array

C =

[2] 'abc' [3.4500]

12

Cell Arrays, cont.

But when deleting elements from cell arrays, you want to get rid of the entire cell – including the wrapper. So use parentheses:

>> C(1) = [] %delete the first container

C =

'def' [3.4500]

13

The same rule applies to these arrays. You can delete elements as long as the overall array remains rectangular! There is no deleting one cell from a 2 dimensional cell-array!

Cell Arrays, cont.

WOW! Cell arrays have many different syntaxes:

Creating: Braces { }Building: Brackets [ ]Referencing: Braces { }Replacing: Braces { }Deleting: Parentheses ()

14

USING CELL ARRAYS

1. Tables of data

2. GUI (Graphical User Interfaces)

15

Real life#1 - tables One of last year’s project calculated the friction of a stunt man on a

surface. However, the user had the choice to choose two surfaces (the clothing the stuntman wore, and the material he/she was sliding to), and whether it was a dry or wet surface. All was contained in 1 variable:

16

Material 1 Material 2

leather leather

leather concrete

leather wood

leather clay

leather weed

Friction

dry wet

0.9 0.1

0.7 0.4

0.8 0.3

0.6 0.2

0.61 0.52

strings.. Numerical data..

Strings..

A BIG CELL-ARRAY!

Real life #2 - GUI

17

Dialog Boxes

Dialog boxes are “popup windows” that allows us 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()

18

Dialog Boxes, cont.

Dialog boxes can be:

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

19

Dialog Boxes, cont.

Input boxes are generally modal – we want the program to wait for input before continuing. This isn’t changeable.

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

20

Dialog Boxes, cont.

Input dialog boxes: Collect information from the user

inputdlg() – much like the input() function, except there can be multiple prompts and multiple values provided by the user. ALL user-provided information is returned as a cell array of strings.

That’s why you need to learn cell-arrays!21

Dialog Boxes, cont.

inputdlg()

Required Arguments:1: Cell array of strings: This is the set of prompts for the user

prompts={'Coeff "a":', 'Coeff "b":', 'Coeff

"c":'}

Return Values:1: Cell array of strings: What the user provided in the boxes

22

Dialog Boxes, cont.

Example code:

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

coeffs = inputdlg(prompts);

If you are collecting numbers, you will probably use str2double() to convert the cell array into a vector:

coeffs = str2double(coeffs);

Note that the prompt strings are stored in a cell array…Does the variable have to be “prompts”?

23

Dialog Boxes, cont.Example program: Quadratic Formula

clearclcprompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};a = 0; b = 2; c = 3; %wrong values to make loop run once % No imaginary roots, or invalidwhile ((b*b - 4*a*c) < 0 || a==0)

% Collect the coeff’s from the user coeffs = inputdlg(prompts); coeffs = str2double(coeffs); a = coeffs(1); b = coeffs(2); c = coeffs(3); end

%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: Do we have to do this, or did we do it for convenience?

24

Dialog Boxes, cont.

MATLAB figures (various types of windows: dialog boxes, plots, etc) have many, many options available to them: position, size, resizable, etc.

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

F1 = Help

25

Recommended