Sundermeyer MAR 999 Spring 2009 1 Laboratory in Oceanography: Data and Methods MAR599, Spring 2009 Miles A. Sundermeyer Data Handling in Matlab.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Programming Environment S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Cells and Structures.
Computer Science 121 Scientific Computing Winter 2012 Chapter 5 Files and Scripts.
Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs.
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
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 Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Lecture 5 Arrays A way to organize data © MIT AITI 2003.
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
Chapter 8 Arrays and Strings
JavaScript, Third Edition
Introduction to MATLAB
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
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Introduction to Python
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1.
C Tokens Identifiers Keywords Constants Operators Special symbols.
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)
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
Chapter 8 Arrays and Strings
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays, File Access, and Plotting
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.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Digital Image Processing Lecture4: Fundamentals. Digital Image Representation An image can be defined as a two- dimensional function, f(x,y), where x.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
Structured Programming I – Relational & Logical Operators Objectives: By the end of this class you should be able to: Change graph interactively in EXCEL.
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.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Matlab Basic. MATLAB Product Family 2 3 Entering & Quitting MATLAB To enter MATLAB double click on the MATLAB icon. To Leave MATLAB Simply type quit.
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.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
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()
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Fall 2006AE6382 Design Computing1 OOP Programming in Matlab Review our ASSET class and study how it might be used as a parent for other classes Extend.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
An Introduction to Programming in Matlab Emily Blumenthal
Characters and Text You can assign text strings to MATLAB variables using single quotes. For example, c = 'Hello'; The result is not a numeric matrix or.
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
MATLAB: Structures and File I/O
funCTIONs and Data Import/Export
INTRODUCTION TO MATLAB
Working With Data.
Presentation transcript:

Sundermeyer MAR 999 Spring Laboratory in Oceanography: Data and Methods MAR599, Spring 2009 Miles A. Sundermeyer Data Handling in Matlab

Sundermeyer MAR 999 Spring Data Handling Matlab Programming Fundamentals / Overview of Matlab Classes Fundamental Classes Many different types of data – generally cannot mix types within single arrays

Sundermeyer MAR 999 Spring Data Handling Matlab Programming Fundamentals / Overview of Matlab Classes Examples of Different Data Types % Create arrays of different data types - see what happens when combined % double and single A = [sin(0:2*pi/9:2*pi)]% double by default A_single = single(A)% create a single AA = [A; A_single]% combines to one data type % reverts to the simpler data type whos % int8 A_int8 = int8(A)% create a signed integer(8) array AA = [A; A_single; A_int8]% combines to one data type % reverts to the simpler data type whos

Sundermeyer MAR 999 Spring Examples of Different Data Types (cont’d) % logical A_logical = logical(A)% all non-zero real elements become 1, % zeros become 0, nans not allowed A_logical = A>0% using logical operators disp(A)% alternate way of displaying to workspace disp(A_logical) AA = [A A_logical]% show the two different logical arrays % default here is to convert logical to double whos % char A_char = 'drifter.dat'% array of characters to create a string whos Data Handling Matlab Programming Fundamentals / Overview of Matlab Classes

Sundermeyer MAR 999 Spring Examples of Different Data Types (cont’d) % cell arrays Acell{1} = A Acell{2} = A_single Acell{3} = A_int8 Acell{4} = A_logical Acell{5} = A_char whos % show indexing of cell array - note curly brackets, {}'s, as well as ()'s Acell{1}(1:5) A(1:5) Data Handling Matlab Programming Fundamentals / Overview of Matlab Classes

Sundermeyer MAR 999 Spring Examples of Different Data Types (cont’d) % structures Astruct.double = A Astruct.single = A_single Astruct.int8 = A_int8 Astruct.logical = A_logical Astruct.char = A_char whos % show indexing of structure Astruct.int8(1:5) A_int8 NOTE: Cannot use Matlab functions like ‘sum’ across cells or structures – this is trade-off between regular arrays and cells & structures. Data Handling Matlab Programming Fundamentals / Overview of Matlab Classes

Sundermeyer MAR 999 Spring Complex Numbers Can be single or double (not int or logical) e.g., % Complex Numbers A_cmplx = 2+A*i% complex double whos A_cmplx = 2+A_single*i% complex single whos A_cmplx = 2+A_logical*i% other types converted to double complex whos A_cmplx = 2*A_int*i% int not allowed with complex operations Data Handling Matlab Programming Fundamentals / Overview of Matlab Classes

Sundermeyer MAR 999 Spring Creating Arrays of Characters and Strings % More on characters and strings clear str = 'Hello'; whos % Examples msg = 'You''re right!' name = ['Thomas' ' R. ' 'Lee'] S = {'Hello' 'Yes' 'No' 'Goodbye'} C = ['Hello '; 'Yes '; 'No '; 'Goodbye'] name = strcat('Thomas',' R.',' Lee') C = strvcat('Hello','Yes','No','Goodbye') Data Handling Matlab Programming Fundamentals / Characters and Strings

Sundermeyer MAR 999 Spring How Matlab uses numbers for characters % can use ‘uint8’ or ‘double’ to convert characters to their numeric values: str_numeric = uint8(str) % The char function converts the integer vector back to characters: str_alpha = char([ ]) % Show a bunch of characters as an array based on numeric values char(32:126)% basic ascii characters char(170:255)% various special characters % Reveal what this array looks like as numbers C = strvcat('Hello','Yes','No','Goodbye') uint8(C) Data Handling Matlab Programming Fundamentals / Characters and Strings

Sundermeyer MAR 999 Spring Data Handling Matlab Programming Fundamentals / Characters and Strings Constructing String Arguments in Code % Constructing String Arguments in Code % Strings can be combined as arrays in same way as numbers e.g., for d=1:3 thisfnm = ['drifter' int2str(d) '.dat'] %load(thisfnm) end % The ‘eval’ Function % Works with string variables to evaluate strings as Matlab commands, e.g., for d=1:3 s = ['load drifter' int2str(d) '.dat'] %eval(s) end

Sundermeyer MAR 999 Spring Loading and Saving Data % Matlab formatted binary data whos save test_data A A_char% save the data A A_char in file test_data.mat what% look to see if it's there % clear all% clear the Matlab workspace who load test_data A A_char% reload the data load('test_data','A','A_char')% accomplishes same as previous line fnm = 'test_data'; load(fnm)% same again whos% check for the reloaded data % Note: 'save' without any arguments saves all variables in file 'matlab.mat' % 'load' without any arguments loads entire contents of.mat file % Note: Can also use '*' wildcard commands with 'load' and 'save'

Sundermeyer MAR 999 Spring Loading and Saving Data % ascii save test_data.dat -ascii A load test_data.dat% puts data in variable A = load('test_data.dat')% puts data in variable A % Note: The following is problematic since the arrays are different sizes % and types - better to use Matlab format in this case save test_data.dat -ascii A A_char % load test_data.dat% gives error

Sundermeyer MAR 999 Spring Loading and Saving Data % Structures % save a structure as it's component variables in a.mat file save test_data -struct Astruct whos -file test_data% list contents of file 'test_data.mat‘ % ASIDE: what 'best practices' rule have I broken in the above example? % load above data as a structure S = load('test_data') whos S% look at properties of variable S S% look at structure elements

Sundermeyer MAR 999 Spring Loading and Saving Data %CSV (Comma Separated Values) % load ONichols_2008_V1.csv% gives error due to header S = csvread('ONichols_2008_V1.csv',1,0)% indices for read are zero based % alternative using importdata S = importdata('ONichols_2008_V1.csv')% more general wrapper for various % file type reads S fieldnames(S) S.data S.textdata S.colheaders

Sundermeyer MAR 999 Spring Loading and Saving Data % More CSV (Comma Separated Values), but with characters mixed in % load ONichols_2008.csv% gives error due to header load ONichols_2008_noheader.csv % still has trouble with character '/' % although no error is encountered ONichols_2008_noheader(1,:)% note 1st column is wrong % Import the file S = importdata('ONichols_2008.csv',',',1); header = S.textdata(1,:) [month day year] = datevec(S.textdata(2:end,1)) % see also datenum and datestr SS = datenum(S.textdata(2:end,1)) datestr(SS) % Write to a csv file dlmwrite('ONichols_2008_out.csv',S.data,'delimiter',',')

Sundermeyer MAR 999 Spring Loading and Saving Data % XLS files fnm = 'SHabtes_ABT_2003_data.xls' [A, DESCR, FORMAT] = xlsfinfo(fnm) [NUMERIC,TXT,RAW]=xlsread(fnm,DESCR{1}) whos % Note: NUMERIC contains all cell values that Matlab could interpret as numeric %TXT contains all cell values that Matlab interpreted as text, in this %case just the header line, so TXT is a 1x8 cell array % RAW contains everything Matlab interpreted as non-empty cell data

Sundermeyer MAR 999 Spring Loading and Saving Data % XLS files again fnm = 'CPeptitas_Silicate_diatom.xlsx' [A, DESCR, FORMAT] = xlsfinfo(fnm)% gives read error for xlsx files [NUMERIC,TXT,RAW]=xlsread(fnm,DESCR{1}) fnm = 'CPeptitas_Silicate_diatom.xls' [A, DESCR, FORMAT] = xlsfinfo(fnm) [NUMERIC,TXT,RAW]=xlsread(fnm,DESCR{1}) whos

Sundermeyer MAR 999 Spring Data Handling Matlab Useful Tidbits … Useful Tidbits clear - clears variables from your workspace (‘clear all’ clears all variables and functions) disp( )- display variables or strings inside.m files help fileformats- see various I/O formats Matlab can handle