1 / 47

Cell Arrays

Cell Arrays. Definition Creating Cell Arrays Referencing Cell Arrays Augmenting Cell Arrays Use of Cell Arrays. Data Types. Recall the workspace frame in MATLAB. Currently, 3 types of data have been seen. They are called: This lecture teaches a new data type called cell arrays.

hollie
Download Presentation

Cell Arrays

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. Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays Augmenting Cell Arrays Use of Cell Arrays

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

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

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

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

  6. 2. Creating Cell Arrays, cont. • Visualize them using cellplot()!

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

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

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

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

  11. 3. Referencing Cell Arrays A 1x1 Cell Array containing a 1x4 array of doubles Curly Braces Content in the cell

  12. 3. Referencing Cell Arrays >> x = cellmat(1,1); >> y = x+5; Undefined function 'plus' for input arguments of type 'cell'.

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

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

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

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

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

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

  19. 5. More Operations… • Given C = {1, 2, ‘Joe’, 3.4, ‘def’} • Delete the 5th cell • Emptying contents of the 2nd cell • Replace 3rd cell with the cell of content ‘sam’; • Change content of 1st cell to the number 8 • Transpose, ‘, still works. C(5) = [] C{2} = [] C(3)= cellstr(‘sam’) OR C{3} = ‘sam’ C{1}= 8

  20. 6. 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?

  21. Introduction to File I/OHigh-Level Functions Data files “High level” File I/O dlmread() xlsread()

  22. 1.1. File Applications Databases Logs – attendance, events, history Journals / Diaries Address books Sensor data Documents Almost every program uses files! Shuttle ECO sensor Automotive O2 sensor

  23. 1.2 Data Files • Data files can be different types and with different data organization

  24. 1.2 Data Files, cont. • Flagler Property Sales

  25. 1.2 Data Files, cont. • Grade

  26. 1.3 File I/O • There are basically three operation modes on files: • Read from: to grab/load data from a file and pass them to variables (INPUT) • Write to: to store/save data to a file from the file beginning (OUTPUT) • Append to: to add/save data to the end of a file (OUTPUT) • In general, 3 steps are always involved: • MATLAB opens the file; • MATLAB reads from, writes to, or appends to the file; • MATLAB close the file;

  27. 2.1 High-level File I/O • A file I/O function is considered HIGH-LEVEL if in ONE SINGLE COMMAND, it • Opens the file • Reads from the file, writes to the file, or appends to the file • Closes the file • “Low-level” I/O functions are to be introduced next week. Those will require more lines of code!

  28. 2.2 Can High-level be used? • To decide if using a high level function is possible, evaluate the following: • What type of file is it? (Excel, text, jpg..) • Recognize the data types (all numerical? all strings? Or combination of both) • Find the organization overall (data in rows, or data in columns, data placed all over the place…) • Recognize the delimiters: What makes it obvious it is a new column? What makes it obvious it is a new row? • space, tabs, comma, new lines, dash, colon, /, any specific symbol!

  29. 2.2 Can High-level Be Used?

  30. 3.1 ASCII Delimited Files • “Neatly Organized” • Rows and Columns are “identifiable” • Always the same patterns per line • There is no mixes of delimiters (commas, spaces, tabs, dash.. ) A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams

  31. Questions • Can these files be read using dlmread()?

  32. The data files (.txt, .xls, .xlsx, .m) should all be in the same directoryfor function calls to work!

  33. 3.2 dlmread() Syntax M = dlmread(filename); Reads numeric data from the ASCII delimited filefilename, and returns the data in output matrix M. The input filename is a string enclosed in single quotes. The delimiter is inferredfrom the formatting of the file. M = dlmread(filename, delimiter); Reads numeric data from the ASCII delimited file filename using the delimiter delimiter such as '-',':'and etc. (Use '\t'to specify a tab.) • >>doc dlmread <enter> open further possible syntaxes

  34. 3.3 Specific Delimiters, cont. • Delimiter: other than the defaults Specify the delimiter as the 2nd argument.

  35. 3.3 Using Delimiters, cont. • BAD… Mix of delimiters. Two delimiters (spaces and colons) in the file. MATLAB is lost. >> “low-level” functions are necessary for this

  36. Example1: airfoils • The NACA airfoils are airfoils shapes for aircraft wings. Load the data (x and y coordinates) for this model of NACA airfoil and plot it. www.angelfire.com

  37. Example1: airfoils clc clear %load the numerical data file %slice the array to extract x and y coordinates % plot the airfoil and format the plot plot(x,y) title('Naca2410') %add plot title axis equal %make axis equal in scale grid on %put the grid on Remember those quotes and the extension. array = dlmread('naca2410.dat'); x = array(:,1); y = array(:,2);

  38. without files… clc clear %prompt how many sets of x-y coordinates sets = input('How many sets (>5)? '); while sets<=5 || mod(sets,1)~=0 %decimals sets = input('ERROR: How many sets (>5)? '); end %prompt user for x/y coordinates for k = 1:sets %ask for x fprintf('x%d: ',k); x(k) = input(''); %ask for y fprintf('y%d: ',k); y(k) = input(''); fprintf('\n'); end % plot the airfoil and format the plot plot(x,y) title('Naca2410') %add plot title axis equal%make axis equal in scale grid on%put the grid on

  39. 2. xlsread()

  40. 2. xlsread(), Syntax Syntax [num,txt,raw] = xlsread(filename) reads data from the first worksheet; [num,txt,raw] = xlsread(filename, sheet) reads the specified worksheet. Numeric data in the spreadsheet are returned to array num. Optional: Textual information in the spreadsheet is returned to cell array txt, and the unprocessed data (numbers and text) to cell array raw. filename is a string. If hardcoded, it must be in single quotes.

  41. Example: xlsread() • Load the grade data from a spreadsheet and find the name of the student who has the highest grade. >>[age_grade, text, raw] =xlsread('grades.xlsx’)

  42. Example: xlsread() >> [age_grade, myText, raw] = xlsread('grades.xlsx') age_grade = 19 78 22 83 98 99 21 56 23 89 19 51 myText= 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy’ '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' '' raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51] 6X2 double 7X3 cell 42 7X3 cell

  43. Example: xlsread() • variable names don’t matter to MATLAB. It respects its order. >> [myText, age_grade, raw] = xlsread('grades.xlsx‘) myText= 19 78 22 83 98 99 21 56 23 89 19 51 age_grade = 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy’ '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' '' raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51] 43

  44. Example: xlsread() • Now find student who has the highest grade. age_grade = 19 78 22 83 98 99 21 56 23 89 19 51 text= 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy’ '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' '' raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51]

  45. Example: xlsread() • Find the student who has the highest grade %load data from file [age_grade, text] = xlsread('grades.xlsx' ); %find the position of the maximum grade [trash row]= max(age_grade(:,2) ); %find whose name is associated with that position name = text{row+1,1} ; %+1 due to headers fprintf('%s got the highest grade\n' , name) • NOTE: raw would be useless here. We simply did not collect it…

  46. xlsread(), cont. • OMIT collecting the 2nd and 3rd return variables if only numerical values are of interest! age_grade = xlsread(‘grades.xlsx’); • If a project needs all the data together, collect the 1st and 2nd return values into a dummy variable. [trash trash data] = xlsread(‘grades.xlsx’); or since MATLAB R2009b, use tilde (~): [~, ~, data] = xlsread(‘grades.xlsx’); • If there happens to be ‘holes’ in the spreadsheet, MATLAB fills it with a NaN value (not a number). The function isnan()can help determine where those ‘holes’ are.

  47. Testing habits • Though the file may contain 1,000,000 lines, and ONE command does the job, it is best to test the analysis with a smaller file: • create a copy of the original file, where only 5-10 lines are present.

More Related