% ECE 1250 S12 Lecture 4

Draw lines showing connecting the operator to the description:  pp. 2-12 to 2-13
.*    Addition (+)
 ^    Subtraction (-)
 /    Multiplication (*)
 '    Division (/)
 \    Left division (\)
 +    Power (^)
 *    Complex conjugate transpose (')
()    Element-by-element multiply (.*)
.'    Element-by-element division (./)
./    Transpose (.')
 -    Specify order of evaluation ( () )

%-------------------------------------------------------------------------------
% Eye function.  Matlab� Primer pp. 3-9
%-------------------------------------------------------------------------------
Ident_mat = eye(3)   % Create identity matrix.
Ident_mat =
     1     0     0
     0     1     0
     0     0     1

% Functions like eye, rand, ones, etc. allow for creation of a square matrix
%  when only one argument is used, or a specified number of rows and cols if
%  two arguments are used.
eye(2,3)
ans =
     1     0     0
     0     1     0

%-------------------------------------------------------------------------------
% Functions with multiple return values, such as min and max p. 1-17 and size.
%-------------------------------------------------------------------------------
A = magic(3)
A =
     8     1     6
     3     5     7
     4     9     2

[max_val, index_of_max] = max(A)    % finds max of cols
max_val =
     8     9     7
index_of_max =
     1     3     2
% We choose the names of the returned variables such as max_val and index_of_max





%-------------------------------------------------------------------------------
% Size command p. 1-11, 2-29, length command p. 2-36.
%-------------------------------------------------------------------------------
B = [1, 2]
B =
     1     2

[n_rows, n_cols] = size(B)
nrows =
     1
ncols =
     2

%-------------------------------------------------------------------------------
% character strings pp. 1-15 to 1-16
%-------------------------------------------------------------------------------
% We can create character variables.
a = 'Hi'

% Need an apostrophe inside the string?  Use two apostrophes in a row.
b = 'Let''s do this!'  % How does Matlab know we are not done with the string
                       %  after the first ' ?  Answer: it sees another ' .
                       % If there is an odd number of ' you are in the string.

n = num2str('15')     % Convert a string into a number.
n = 15

% We can concatenate strings.  Strings are just arrays.
long_str = ['There are ', num2str(n), ' students in the lab']
% Be careful t put spaces in the string.

% A simple command for displaying strings:
display('long_str')

%-------------------------------------------------------------------------------
% Advanced indexing.  (See handout on website, pp 2 to end.)
%-------------------------------------------------------------------------------

%-------------------------------------------------------------------------------
% Writing script files.
%-------------------------------------------------------------------------------
% Change directory to where script files are located so Matlab� can find them,
%  or use the Set Path command under the File menu to add the folder with
%  the script files to where Matlab� looks for commands.

% Use .m extension on file name.  Put in commands just as you would at the
%  >> prompt in the command window.  Here's a simple example of a script file
%  we shall call my_magic.m
A = magic(3)
display('That''s a magic square!')


% To execute the script file my_magic.m, for example, type the filename without
%  the .m
>> my_magic
A =
     8     1     6
     3     5     7
     4     9     2
That's a magic square!

% Be careful not to create files with the same name as a Matlab� command.
% For example, say you create a file called plot.m.  When you use the plot
%  command, Matlab� will execute the first plot.m file it finds in the
%  search path.  If your plot.m was found first, you would no longer have
%  access to Matlab�'s plot command.

% How to get rid of your plot.m command once it gets into Matlab� (and gets
%  compiled and stored in memory so the file is no longer accessed directly)?
% 1) Remove or rename your file.
% 2) Use the clear command to clear the filename, e.g., >> clear plot
% The next time you type "plot", Matlab� will go through the search path
%  again and find the correct file.

% Now for some example script files.
% Ex: Script file to flip the second half of a sound waveform.

% Ex: Calculate and display 30 msec snippets of sound waveform, y.  Shift by
%      10 msec each time program runs.  Note: sampling rate is 8000 samp/sec.
%     Corresponds to windows used for spectral analysis in speech recognition.
%     Also, find the "energy" in each 30 msec snippet by finding the average
%      value of the sum of squares of the samples.
%     Also, find the location and value of the max of the waveform.
%     Also, plot the snippet each time.