## MATLAB Notes

Yet another program like IDL and R with similar function but more mathematical emphasis, and slightly different syntax for everything.

```
A. BASIC SYNTAX
cd                         % show current directory
cd ..                      % like unix, move up a directory
cd /home/hartigan/programs % change directory
ls                         % like unix, shown in side window
open file.dat              % read in data from a file
%                          % the comment character
x                          % prints x to screen
clear                      % clear variables from workspace (listed in right panel)
save stuff.dat             % save variables from current session
save y y                   % saves vector y into file y.mat, which is a binary format
%  that matlab can open, e.g. via the menu 'open' command
str = ' a string'          % define a character string
str1 = [str ' = ' num2str(a)] % combine strings. num2str converts number to string
doc command                % manual on command
help command               % short help on command
type test.dat              % print contents of test.dat to screen

B. NUMERICAL OPERATIONS
pi*exp(1)                  % pi and e
log10(x)                   % logarithm base 10.  log is natural log
x^3                        % cube of x.   x**3 does not work
x=0:0.5:4;                 % sequence, x=0,0.5,1.0,...4.0
cos(x)                     % basic trig functions work
acos(x)                    % inverse trig functions in radians
max(a)                     % maximum value of a
%
x=[2,5;3,-3]               % define a matrix
inv(x)                     % matrix inverse
det(x)                     % matrix determinant
x'                         % matrix transpose
zeros(3,2)                 % 3x2 matrix of zeros
[x;x;x]                    % stack 3 x's to form a taller matrix
a(2,:)                     % all values in row 2 of array ':' is wildcard
%
rand(30,2)                 % 2 columns of 30 random numbers
%
mean(y)                    % mean of y
var(y)                     % variance of y

C. PLOTTING
plot(x,y)                  % the plot command, black line
plot(x,y,'b--')            % dashed blue line, b-. for dot-dash, g* for green stars
% d for diamonds, k- for black line, etc
xlabel('Wavelength')       % xlabels and ylabels
title('Important Graph')   % title of plot
legend('Temp','Density')   % legend for plot
surf(x,y,x)                % surface plot. use meshgrid to embed grid
subplot(2,2,1); plot commands ;  % plot 1 of 4 in a 2x2 grid
char(176)                  % the degree character

D. PRINTING
E. PROGRAMMING
for k=1:n               % syntax for looping
c(i)=y(3*i)
end
%scripts
file                       % runs script of matlab commands from file.m ; the .m matters
% cannot be named 'try.m' as that is something else in the path

% open a file and read columns of data from it
fid=fopen('name.dat','r');  % 'r' to read, 'w' to write, fid is a logical unit number
a=fscanf(fid,'%f',[5,Inf]); % '%f' is for floating point, 5-column file, %d for integers
col3=a(3,:)'                % column 3 of name.dat
fclose(fid)                 % close the file

frewind(fid);               % rewind file to beginning

pause(0.5)                  % sleep for 0.5 seconds

F. WAVELETS
waveletAnalyzer             %launches wavelet gui; useful for seeing coefficients
%click on Property Editor, then click the axis to change font
% Tools->Data Cursor and click to see values; Right-click to delete
cwt(x,'haar','NumberofOctaves',8,'VoicesperOctave',10,seconds(1))  %Continuous wavelet spectrogram
% Haar is the wavelet, seconds tells it to do periods
% icwt is the inverse transform
```