% Laboratory in Oceanography - Data and Methods % SMAST, UMass Dartmouth % % Examples of assigning different data types in Matlab, and of reading % and writing to and from various formatted data files. % % Written by Miles A. Sundermeyer, 2/9/09 % set a breakpoint - use F10 to step line by line in debug mode keyboard %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 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 %% 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Now store all these different data types in a single cell array 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) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Now store these in a structure 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 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 allowed types convert to double complex whos %A_cmplx = 2*A_int8*i % int not allowed with complex operations %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% More on characters and strings str = 'Hello'; whos % Arrays of chars to create strings % 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') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Show how Matlab uses numbers for characters % can use uint8 or double function 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) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Constructing String Arguments in Code % Strings can be combined as arrays in same way as numbers e.g., for d=1:3 fnm = ['drifter' int2str(d) '.dat'] %load(fnm) 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOADING AND SAVING DATA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% help fileformats %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 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' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 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,:) [year month day] = 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',',') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 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