Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CC SQL Utilities.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Microsoft Office XP Microsoft Excel
Excel Objects, User Interface, and Data Entry. ◦ Application Window  Title Bar  Menu Bar  Toolbars  Status Bar  Worksheet Window  Worksheet Input.
1. Definition 2. Creating Cell Arrays 3. Augmenting Cell Arrays 4. Referencing Cell Arrays 5. Special case 6. Spreadsheets 1.
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003.
Chapter 9: Arrays and Strings
Fall 2006AE6382 Design Computing1 Matlab File & Directory Management Learning Objectives Define file input and output terminology Compare high and low.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
AN ENGINEER’S GUIDE TO MATLAB
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
1 Functions Advantages Name, parameters, return info Flow of the data when running functions Menus Functions Applying Functions to Real Projects.
Lists in Python.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
File I/O 1. Goals of this chapter 2. Inputs Files 3. Output Files 4. File Formats 5. General Concept, lots of examples 6. High-Level vs. Low-Level 1.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
File I/O 1. Goals of this chapter 2. General Concept, lots of examples 3. High-level I/O 1. Built-in functions that do everything in one step 4. Low-level.
Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
CMPS 1371 Introduction to Computing for Engineers FILE Input / Output.
File I/O High-Level Functions 1. Definition 2. Is a High-Level function appropriate? 3. xlsread() 4. dlmread() 1.
Logical Ops on Arrays Saving time / avoiding loops 1. Logical Operators 2. Logical Operations 3. Examples: numbers and letters 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
Built-in Data Structures in Python An Introduction.
1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Asking the USER for values to use in a software 1 Input.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Asking the USER for values to use in a software 1 Input.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
Cell Arrays Lecture 2/8/ Data Types (Review) 2. General Concept 3. Using Cell-Arrays 1. Syntax/Symbols 2. Dialog Boxes 1.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Strings Characters and Sentences 1.Overview 2.Creating Strings 3.Slicing Strings 4.Searching for substrings 1.
Sundermeyer MAR 999 Spring Laboratory in Oceanography: Data and Methods MAR599, Spring 2009 Miles A. Sundermeyer Data Handling in Matlab.
An Introduction to Programming in Matlab Emily Blumenthal
NXT File System Just like we’re able to store multiple programs and sound files to the NXT, we can store text files that contain information we specify.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Containers and Lists CIS 40 – Introduction to Programming in Python
IMPORTING AND EXPORTING DATA
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
MATLAB: Structures and File I/O
Jeff Henrikson Lecture 11 Data Import & Export Jeff Henrikson 1.
Advanced Data Import & Export Jeff Henrikson
funCTIONs and Data Import/Export
INTRODUCTION TO MATLAB
Introduction to MATLAB
Presentation transcript:

Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1

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

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

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

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!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1.2 Data Files, cont. Flagler Property Sales 24

1.2 Data Files, cont. Grade 25

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

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

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

2.2 Can High-level Be Used? 29

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

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

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

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

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

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

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

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.

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

2. xlsread() 39

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

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 age_grade = 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 myText = 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‘)

Example: xlsread() Now find student who has the highest grade. 44 age_grade = 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]

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

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

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