Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Sundermeyer MAR 999 Spring 2009 1 Laboratory in Oceanography: Data and Methods MAR599, Spring 2009 Miles A. Sundermeyer Data Handling in Matlab."— Presentation transcript:

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

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

3 Sundermeyer MAR 999 Spring 2009 3 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

4 Sundermeyer MAR 999 Spring 2009 4 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

5 Sundermeyer MAR 999 Spring 2009 5 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

6 Sundermeyer MAR 999 Spring 2009 6 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

7 Sundermeyer MAR 999 Spring 2009 7 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

8 Sundermeyer MAR 999 Spring 2009 8 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

9 Sundermeyer MAR 999 Spring 2009 9 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([72 101 108 108 111]) % 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

10 Sundermeyer MAR 999 Spring 2009 10 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

11 Sundermeyer MAR 999 Spring 2009 11 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'

12 Sundermeyer MAR 999 Spring 2009 12 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

13 Sundermeyer MAR 999 Spring 2009 13 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

14 Sundermeyer MAR 999 Spring 2009 14 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

15 Sundermeyer MAR 999 Spring 2009 15 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',',')

16 Sundermeyer MAR 999 Spring 2009 16 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

17 Sundermeyer MAR 999 Spring 2009 17 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

18 Sundermeyer MAR 999 Spring 2009 18 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


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

Similar presentations


Ads by Google