Chapter 1 Matlab Overview EGR1302 Desktop Command window Current Directory window Tb Tabs to toggle between Current Directory & Workspace Windows Command History window 1
Desktop Default appearance Command window Communicates with MATLAB program Type commands Receive response to your commands Displays >> prompt MATLAB is ready to receive instructions User types command, presses Enter to execute ; at end of command suppresses response Retains previous keystrokes Use key to scroll backward Use key to scroll forward Edit with,, Backspace, Delete Desktop Default appearance Command History window Shows all previous keystrokes Copies previous command to Command window Left-click and drag to Command window Double-click to re-execute command 2
Desktop Default appearance Current Directory window Serves as file manager window Double-click on.m file to open Editor window Workspace window Lists variables created in Command window Double-click on variable name to open Array Editor window Desktop Default appearance Row of menu names Each window type has its own menu bar Toolbar Shortcut buttons Current directory box Change to directory in which you save your files Provides path for Current Directory window 3
Variables A symbol used to contain a value Used to write mathematical expressions Variable naming convention Must begin with a letter Must contain < 32 characters Is case sensitive May contain letters, digits, & underscore Scalar variable - a single number Array variable - a collection of numbers Can be handled by MATLAB as a single variable Variables Assigning values to variables Use = as assignment operator Variable name on left-hand side is replaced by value generated by right-hand side Right-hand side must have computable value Array assignment [] contains numbers being assigned Commas or spaces separate elements in row Semi-colons separate rows 4
Variables Array assignment (cont) Special format for assigning row array with regularly l spaced numbers Type first number, last number, with spacing in middle All numbers separated by colon : u = [ 0 : 0.1:10 ] Variables Array addressing Use just variable name in expressions Operation is performed on every element in the array IF matrix operation is legal Use variable name & array index in expressions Point to a particular element in the array Use if operation to be performed is not legal matrix operation u(7) 5
Variables MATLAB has predefined variables Avoid using names of predefined variables as variable names Mathematical Expressions Order of precedence Highest order of precedence first Operators of equal precedence from left to right 6
Functions MATLAB mathematical functions Pair of parentheses after function name encloses function s argument Can be part of mathematical expression Functions Users can define functions MATLAB commands and functions for managing the work session 7
Plotting Graphics window Appears in Desktop when a plot is generated Plotting Simple rectilinear plot Function plot(x,y) Generates a plot with x-values on the horizontal axis and y-values on the vertical axis Function xlabel( xxxx ) Places text within single quotes as label for horizontal axis Function ylabel( yyyy ) Places text within single quotes as label for vertical axis 8
Plotting Simple rectilinear plot (cont) Function title( text ) Places text at top of plot Overlay plots Include another set or sets of values in plot function Script Files Program files saved with.m extension Use Editor window 9
Script Files Naming convention Same as for naming variables Not the same name as a variable or a MATLAB function Use exist( name ) function to verify that a prospective filename is not already in use Script Files Comments % - Not executed by MATLAB Comments section at beginning of program 1 st line - name of the program, keywords 2 nd line - date created and programmer s name 3 rd line description of program s purpose Variable definitions Input Output Internal to program 10
Script Files Comments Additional comments throughout program Break program into sections. Describe purpose for calculations Programming in MATLAB Relational operators Used to make comparisons Script Files Programming in MATLAB (cont) Conditional statements if expression Allow decision-making commands elseif expression commands else commands end 11
Script Files Programming in MATLAB (cont) Loops Repeated calculations For Number of passes is known ahead of time While Looping terminates when a condition is met for k=1:10 commands end while condition commands end 12