280 likes | 413 Views
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.
E N D
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 • 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
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
Quick Vocabulary Parentheses ( ) Brackets [ ] Braces { }
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!
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.
2. Creating Cell Arrays, cont. • Cell arrays can be of higher dimensions a 2 by 2 cell array
Test your understanding Is this cell array, A = {1:4; 2:3}, rectangular? Answer: Yes, it is rectangular. Its size is 2 by 1.
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
3. Referencing Cell Arrays Parentheses property of the cell is shown, not content
3. Referencing Cell Arrays Curly Braces Content in the cell
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'.
3. Referencing Cell Arrays >> x = cellmat(1,1); >> y = x+5; ✗ >> x = cellmat{1,1}; >> y = x+5; y = 6 8 10 12
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.
3. Referencing Cell Arrays >> fprintf('%d\n',cellmat{2,1}); 20 >> fprintf('%s\n', cellmat{2,2}); Hello
3. Referencing Cell Arrays Cell indexing: ( ) Content indexing: { }
4. Cell Arrays – Augmenting • Add the string 'def'. ??
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...
4. Cell Arrays – Augmenting • Instead, augment using square brackets
4. Cell Arrays – Augmenting • Add a row? Of course! • Like with arrays, use ; to add a new row.
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.
5. Slicing Cell-Arrays • Slicing the ages "slicing the cells" "slicing the content" ✗ data
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';
6. More Operations… (cont.) • Using a cell-array of numerical values???? x
6. More Operations… (cont.) • Convert a cell-array of numbers to a matrix: cell2mat() x1 x
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?
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