Download presentation
Presentation is loading. Please wait.
Published byBetty Johnson Modified over 9 years ago
1
Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1
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 2 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… 3 Simply It is an ARRAY of CELLS
4
Quick Vocabulary Parentheses ( ) Brackets [ ] Braces { } 4
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 5 Curly braces – not brackets!
6
2. Creating Cell Arrays, cont. Visualize them using cellplot()! 6 Joe
7
2. Creating Cell Arrays, cont. Cell arrays can be of higher dimensions 7 a 2 by 2 cell array
8
Question Is this cell array, A = {1:4; 2:3}, rectangular? 8 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 9
10
3. Referencing Cell Arrays 10 Parentheses property of the cell is shown, not content
11
3. Referencing Cell Arrays 11 Curly Braces Content in the cell A 1x1 Cell Array containing a 1x4 array of doubles
12
3. Referencing Cell Arrays 12 Undefined function 'plus' for input arguments of type 'cell'. >> x = cellmat(1,1); >> y = x+5;
13
3. Referencing Cell Arrays 13 >> x = cellmat(1,1); >> y = x+5; ✗ >> x = cellmat{1,1}; >> y = x+5; ✔ y = 681012
14
3. Referencing Cell Arrays 14 Cell indexing: ( ) Content indexing: { }
15
4. Cell Arrays – Augmenting Add the string 'def'. 15 ??
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 16 NOT what we wanted...
17
4. Cell Arrays – Augmenting Instead, augment using square brackets 17
18
4. Cell Arrays – Augmenting Add a row? Of course! Like with arrays, use ; to add a new row. 18
19
5. More Operations… Given C = {1, 2, ‘Joe’, 3.4, ‘def’} – Delete the 5 th cell – Emptying contents of the 2 nd cell – Replace 3 rd cell with the cell of content ‘sam’; – Change content of 1 st cell to the number 8 – Transpose, ‘, still works. 19 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? 20
21
Introduction to File I/O High-Level Functions 1.Data files 2.“High level” File I/O 3.dlmread() 4.xlsread() 21
22
1.1. File Applications Databases Logs – attendance, events, history Journals / Diaries Address books Sensor data Documents Almost every program uses files! 22 Shuttle ECO sensor Automotive O 2 sensor
23
1.2 Data Files Data files can be different types and with different data organization 23
24
1.2 Data Files, cont. Flagler Property Sales 24
25
1.2 Data Files, cont. Grade 25
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; 26
27
2.1 High-level File I/O A file I/O function is considered HIGH-LEVEL if in ONE SINGLE COMMAND, it 1.Opens the file 2.Reads from the file, writes to the file, or appends to the file 3.Closes the file “Low-level” I/O functions are to be introduced next week. Those will require more lines of code! 27
28
2.2 Can High-level be used? To decide if using a high level function is possible, evaluate the following: 1.What type of file is it? (Excel, text, jpg..) 2.Recognize the data types (all numerical? all strings? Or combination of both) 3.Find the organization overall (data in rows, or data in columns, data placed all over the place…) 4.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! 28
29
2.2 Can High-level Be Used? 29
30
3.1 ASCII Delimited Files “Neatly Organized” 1.Rows and Columns are “identifiable” 2.Always the same patterns per line 3.There is no mixes of delimiters (commas, spaces, tabs, dash.. ) 30 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 streamscharactersplain text
31
Questions Can these files be read using dlmread() ? 31
32
The data files (.txt,.xls,.xlsx,.m) should all be in the same directory for function calls to work! 32
33
3.2 dlmread() Syntax M = dlmread(filename); Reads numeric data from the ASCII delimited file filename, and returns the data in output matrix M. The input filename is a string enclosed in single quotes. The delimiter is inferred from 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 open further possible syntaxes 33
34
3.3 Specific Delimiters, cont. Delimiter: other than the defaults 34 Specify the delimiter as the 2 nd argument.
35
3.3 Using Delimiters, cont. BAD… Mix of delimiters. 35 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. 36 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 37 array = dlmread('naca2410.dat'); x = array(:,1); y = array(:,2); Remember those quotes and the extension.
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 38
39
2. xlsread() 39
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. 40
41
Example: xlsread() Load the grade data from a spreadsheet and find the name of the student who has the highest grade. 41 >>[age_grade, text, raw] = xlsread('grades.xlsx’)
42
42 age_grade = 19 78 22 83 98 99 21 56 23 89 19 51 myText= 'Name' 'Age' 'Grade' 'Fred' '' '' 'joe' '' '' 'SaLLy’ '' '' 'CharliE' '' '' 'mary' '' '' 'Ann' '' '' >> [age_grade, myText, raw] = xlsread('grades.xlsx') 6X2 double 7X3 cell raw = 'Name' 'Age' 'Grade' 'Fred' [ 19] [ 78] 'joe' [ 22] [ 83] 'SaLLy' [ 98] [ 99] 'CharliE' [ 21] [ 56] 'mary' [ 23] [ 89] 'Ann' [ 19] [ 51] Example: xlsread()
43
43 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] Example: xlsread() variable names don’t matter to MATLAB. It respects its order. >> [myText, age_grade, raw] = xlsread('grades.xlsx‘)
44
Example: xlsread() Now find student who has the highest grade. 44 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… 45
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. 46
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. 47
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.