1 / 28

High Level in 2 parts

High Level in 2 parts. High-Level Functions for File I/O:. dlmread ()  Numerical Array only xlsread ()  Numerical AND/OR cell-arrays variables!!!. Cell Arrays. Definition Creating Cell Arrays Referencing Cell Arrays Augmenting Cell Arrays Slicing Cell Arrays. Data Types.

tamera
Télécharger la présentation

High Level in 2 parts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. High Level in 2 parts High-Level Functions for File I/O: dlmread()  Numerical Array only xlsread()  Numerical AND/OR cell-arrays variables!!!

  2. Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays Augmenting Cell Arrays Slicing Cell Arrays

  3. Data Types • Recall the workspace frame in MATLAB. Currently, 3 types of data have been seen. They are called: • This tutorial teaches a new data type called cell-arrays char (strings) double (float) or int8, int16… logical

  4. 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… Simply, it is an ARRAY of CELLS

  5. Quick Vocabulary Parentheses ( ) Brackets [ ] Braces { }

  6. 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 Curly braces – not brackets!

  7. 2. Creating Cell Arrays, cont. • Visualize them using cellplot()! FYI: It is just a representation. J is in the first box, o in the second, e in the third.

  8. 2. Creating Cell Arrays, cont. • Cell arrays can be of higher dimensions a 2 by 2 cell array

  9. Test your understanding Is this cell array, A = {1:4; 2:3}, rectangular? Answer: Yes, it is rectangular. Its size is 2 by 1.

  10. 3. Referencing Cell Arrays • Like with numerical arrays, row and column indices are used • Unlike numerical 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

  11. 3. Referencing Cell Arrays Parentheses property of the cell is shown, not content

  12. 3. Referencing Cell Arrays Curly Braces Content in the cell

  13. 3. Referencing Cell Arrays This becomes important when wanting to use the values! >> x = cellmat(1,1); >> y = x+5; Undefined function 'plus' for input arguments of type 'cell'.

  14. 3. Referencing Cell Arrays >> x = cellmat(1,1); >> y = x+5; ✗ >> x = cellmat{1,1}; >> y = x+5; y = 6 8 10 12

  15. 3. Referencing Cell Arrays This becomes important when wanting to print values! >> fprintf('%d\n',cellmat(2,1)); Error using fprintf Function is not defined for 'cell' inputs. >> fprintf('%s\n', cellmat(2,2)); Error using fprintf Function is not defined for 'cell' inputs.

  16. 3. Referencing Cell Arrays >> fprintf('%d\n',cellmat{2,1}); 20 >> fprintf('%s\n', cellmat{2,2}); Hello

  17. 3. Referencing Cell Arrays Cell indexing: ( ) Content indexing: { }

  18. 4. Cell Arrays – Augmenting • Add the string 'def'. ??

  19. 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 NOT what we wanted...

  20. 4. Cell Arrays – Augmenting • Instead, augment using square brackets

  21. 4. Cell Arrays – Augmenting • Add a row? Of course! • Like with arrays, use ; to add a new row.

  22. 5. Slicing Cell-Arrays • Remember that the content of 1 cell is NOT connected to the content of another cell. ✗ • Slicing the content is not practical. • However, slicing the cells (i.e. containers) remains the same.

  23. 5. Slicing Cell-Arrays • Slicing the ages "slicing the cells" "slicing the content" ✗ data

  24. 6. More Operations… cellplot(c) • Given c = {7,-2.2,'Joe',3.4,'def'} • Delete the 5th cell c(5) = []; • Emptying contents of the 2nd cell c{2} = []; • Replace 3rd cell with the cell of content 'sam'; c{3}= 'sam'; or c(3)={'sam'}; • Change content of 1st cell to the number 8 c{1}= 8; or c(1)={8}; • Transpose, ', still works. c=c';

  25. 6. More Operations… (cont.) • Using a cell-array of numerical values???? x

  26. 6. More Operations… (cont.) • Convert a cell-array of numbers to a matrix: cell2mat() x1 x

  27. 7. 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?

  28. Key Ideas • Cells arrays are ‘container-ships’ -- arrays of cells that allow storage of different data-types into 1 variable. • Cell-arrays have 3 different syntaxes: Creating: Braces { } Augmenting: Brackets [ ] Referencing content: Braces { } Referencing cells: Parentheses () • Slicing can be a challenge. Do NOT slice the content. • Use cell2mat() to convert a cell-array of numbers to a matrix

More Related