Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1."— Presentation transcript:

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

2  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

3  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

4  Create a cell array by using the {} brackets  Separate each element in the array with a comma 4 Curly braces – not parentheses, not brackets!

5  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.

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

7  { } 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! 7 NOT what we wanted...

8  Augment the same as with normal arrays: use brackets 8

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

10  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

11 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 {}

12  Assume C starts with: %option1: refer to & delete the 2 nd container 12

13  Assume C starts with: %option1: refer to & delete the 2 nd container %option2: refer to & delete the content of the 2 nd container 13

14  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.

15  To use this variable X mathematically creates an error  After all, this is what MATLAB tries to execute: result = 2* + 5 15 ?

16  Recall to use curly braces to access the CONTENT. 16 Use curly braces {}. Notice the lack of [] around the result.

17  To conclude, also use curly braces to replace elements. 17 Initial data-base (name, age, weight) Fix the name

18  What if there is an array within a cell? 18 1) max? 2) last score?

19  What if there is an array within a cell?  "Max of the content of container 2". Use { }. 19 1) max?

20  The following requires patient imagination skills, but it works and makes sense! 20 Simply: "Reference to the 3 rd element in the CONTENT of the 2 nd container" Simply: "Reference to the last element in the CONTENT of the 2 nd container"

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

22  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!

23  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

24  Numbers have different precisions and formats delimiters (spaces, tabs, ;) separate columns Space delimiterComma 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

25  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

26  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_pro g/br04bw6-98.html http://www.mathworks.com/help/techdoc/matlab_pro g/br04bw6-98.html 26 Most likely to be used often!

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

28  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!

29  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

30  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

31  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 …

32  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 Example prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'}; coeffs = inputdlg(prompts); 32 Cell-array, indicated by the curly braces.

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

34 34 clear clc prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'}; flag=1; % No invalid inputs, or imaginary roots while (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; end %calculate roots roots(1) = (-b + sqrt(b*b-4*a*c))/(2*a); roots(2) = (-b - sqrt(b*b-4*a*c))/(2*a)

35 35 clear clc prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'}; flag=1; % No invalid inputs, or imaginary roots while (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; end %calculate roots roots(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?

36 36 clear clc prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'}; flag=1; % No invalid inputs, or imaginary roots while (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; end %calculate roots roots(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.. 

37 37 clear clc prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'}; flag=1; % No invalid inputs, or imaginary roots while (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; end %calculate roots roots(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.

38 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.

39 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

40 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

41 41 The Select All button is gone.

42  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

43  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

44  button = string on the button that was clicked 44

45  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

46 46

47 47

48  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

49  If you want to do more than fundamental dialog boxes, study the MATLAB help documentation.  http://www.mathworks.com/help/techdoc/ref/f 16-40727.html#f16-8026 49 F1 = Help


Download ppt "1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1."

Similar presentations


Ads by Google