## Lab 2: Matlab Programming

As we repeat Lab 1, you should understand the problem, and how you need to tackle it.  The job is to figure out how to do that in Matlab.

## Lab 5.  Cross correlation

Find the max in a vector, and where it occurs (if you just need the max, Matlab does it very easily):

%set the max so low it will definitely be higher

MaxTemp = -99999;

for i=1:length(Temps)     %array to search form the max

if Temps(i) > MaxTemp

MaxTemp = Temps(i);

TimeMaxTemp = TempTime(i);

end;

end;

Getting N-S Wind components:

Do we need to put this in a loop?

Getting other wind direction components: +/- an angle, which moves it to "north" where you can use the code above

• readin = fgetl(inf);       %readin will be a string, containing a complete line from the file "inf"
• substr = readin(23:24);   % makes a substring from columns 23 to 24 of readin; this is the punch card definition of column, and not the Excel version. You can find column by looking at the data in the Matlab editor; the column is in the lower right of the window, and refers to the character to the right of the cursor
• num = num2str(substr);  %turns the substring into a number.

Reading and processing an ASCII  data file, general process:

• open input file
• open output file 'w' if needed
• zero counters
• start while loop through file, one record at a time
• process header if present and required;
• decide what to do with record (if then else) based on header data in the line you read
• increment counters
• extract values like lat/long into vectors
• read and process additional lines with data if present (might just skip, might save to new file, might fill vectors with data values)
• might be constant number of lines per record
• might be variable, and you have to get from the header
• end while loop
• close files
• print tables
• make figures
• clear variables

Comparing strings: Matlab might be fussy about using the "==" operator to test for string equality.  In this case you can use the strcmp function.

• if strcmp(Prof1,"TEMP")......... is the same as if strcmp(Prof1,"TEMP") == 1...........

Pretty printing or in Matlab-speak, smart indenting from the right click on a block of highlighted code.

Logic in processing CTD data

last revision 4/19/2017