% Useful MATLAB commands % Comments % Anything behind a percent symbol is commented out % Help on functions % help function_name help stepfun % MATLAB primarily uses vectors and matrices to store data values A = [1 2 3; 4 5 6]; % Creates a 2x3 matrix % Semicolon in the [] is used for creating a new row % Access individual elements % variable_name(row_value,column_value) A(2,3) % Returns the value 6 % This method can also be used to write to a specific element % Access individual column or row % Use a colon (:) in place of a row or column A(:,1) % Returns only the first column % Creating a large, evenly spaced vector % vector_name = low_value:increment_size:high_value; tt = 0:0.1:10; % Preventing MATLAB from echoing back all values % Put a semicolon at the end of the line % Size of a variable % size(variable_name) size(A) % Returns the number of rows and columns % Transpose a matrix % matrix_name' A' % Point-by-point multiplication and division % MATLAB defaults to matrix multiplication with * % To perform point-by-point multiplication of two vectors, use .* tt_squared = tt.*tt % This also works with division (/) and exponentiation (^) % Determining what variables are present who % lists all variables whos % lists all variables and their sizes % To create a new figure figure; % Or, to create a specific figure number figure(10); % MATLAB will always plot to the previously selected figure % Plotting % plot(independant_variable,dependant_variable); plot(tt,yy); % You can change the color of the line with the optional argument plot(tt,yy,'r') % Type "help plot" for more options % Stem plots % Useful for plotting discrete-time signals % stem(independant_variable,dependant_variable); stem(tt,yy); % Labeling your plots % You must use text strings offset by ' marks xlabel('Time (s)') ylabel('Output') title('The Example Signal') % Multiple graphs in the same figure % subplot(rows_of_plots,columns_of_plots,selected_plot) subplot(2,1,1); % Creates two plots (one above the other) and selects the top % one for plotting % Other useful plotting commands % Create a legend for your plot legend('Signal 1', 'Signal 2') % Change the axes ranges % axis([low_x high_x low_y high_y]) axis([-10 10 0 5]); % Toggle the grid on or off grid % Hold the plot to allow another signal to be plotted % on the same plot hold on; % Printing/saving plots % Plots can be printed directly from the figure window % Plots can also be saved % .fig files are the default from the pull-down menu % .fig files can only be reopened in MATLAB % metafile for use in Word documents and PowerPoint slides % copies the figure to the clipboard print -dmeta % .eps files for use in LaTeX files % stores the image in a .eps file (Encapsulated PostScript) % called file_name.eps print -dmeta file_name % Saving your work % save file_name variable_1 variable_2 ... save ee327_example_1 A tt % Creating .m files % Script files and user-defined functions can be written in .m files % To open a new .m file, click the "New File" button or type edit % Alternatively, you can open an existing .m file by typing edit file_name % Script files (.m files) can contain any code written at the MATLAB prompt % This is useful for editing your work and staying organized % This is also useful for having all the code written neatly for % turning in your homework % You can run a script file by typing its name at the command prompt % You can also run the script by pressing F5 in the editor window % Functions % You can create your own functions by opening a new file and writing % your code. The first line of the function must be function [output_1,output_2] = my_function(input_1,input_2) % Any comments directly following this line will be displayed to the % screen if you type "help my_function" % Then, write all your lines of code % Make sure you write to the output variables