Valuable CommandsThe following commands are generally useful, particularly when first learning Matlab
- help - Provides help on specified topics. May be followed by a specific topic, e.g. "help plot"
- helpwin - Opens up a GUI window to the help system. May also be followed by a specific topic.
- lookfor - ( not in book ) Find functions based on keywords in description
- clear - clears ( deletes ) variables. You can also clear specific variables.
- clc - clears command window
- who, whos - Lists variables, with and without size information
- which - Indicates whether an identifier is a variable, built-in Matlab function, user-defined function, or other
- what - Lists files in the current directory
- ans - The last value calculated
- i, j - square root of -1, UNLESS they have been re-defined
- pi - 3.14159 . . .
- Inf - Infinity
- inf( N ) creates an N x N array of infinity
- inf( M, N ) creates an M x N array of infinity
- NaN - Not a Number, for example zero divided by zero
- Can also generate arrays of NaN
- eps - Epsilon, the smallest number the computer can add to 1.0 and distinguish the sum from 1.0. I.e. the smallest value for which the computer can tell the difference between ( 1.0 + epsilon ) and 1.0.
- % ( percent ) - Indicates the start of a comment, which extends to the end of the line
- ; ( semicolon ) - Suppresses output. Ordinarily every Matlab
command generates a result, and that result is displayed in the command
window. Placing a semicolon at the end of the line causes it to do the
work without displaying the result. In a script, it causes the command
to not be displayed at all.
- Also used to separate rows in a matrix
- ' ( apostrophe ) - Used to quote strings of text.
- Also takes the transpose of a matrix
- ... ( ellipses ) - Placed at the end of a line ...
to indicate that the command continues on the next line
- ( ) - Used for sevaral purposes:
- forcing precedence, e.g. ( 3 + 4 ) / 2
- calling functions
- accessing elements of a matrix. ( i.e. for matrix subscripts )
- NOTE THAT ARRAY INDICES BEGIN AT 1 IN MATLAB,NOT 0
- [ ] - Used for creating matrices
- , ( comma ) - Separates arguments in a function or values in a matrix
- : ( colon ) - Generally used to indicate a range of values, e.g. 5:10. See also basic matrix operations
- May be different for scalars versus arrays versus combinations of both
- +, -
- Addition and subtraction.
- Two matrices must have the same number of rows and columns to be added or subtracted.
- A scalar can be added to or subrracted from an array, in either order. ( Array - scalar or scalar - array ) The opeation is applied element by element.
- *, /
- Multiplication and division
- To multiply two matrices requires that the number of columns in the first matrix match the number of rows in the second.
- An ( M x C ) matrix * a ( R x N ) matrix yields a ( M x N ) result, where R must equal C
- To divide two matrices requires that the have the same number of columns. The numerator should have at least as many rows as the denominator.
- Scalars can be multiplied by or divided by arrays in either order. The operation is applied element-by-element.
- Left division is only relevant to matrix operations:
- A x = b
- x = A \ b
- Requires that the number of columns in the numerator and denominator match. The numerator should have at least as many rows.
- Left division is only relevant to matrix operations:
- Exponentiation, e.g. area = pi * radius^2
- When applied to a matrix, the matrix must be square. I.e. it must be possible to multiply the matrix by itself.
- Elementwise - .*, ./, .^ - Acts on matrices element by element, as opposed to normal matrix operations.
- For example, array.^2 would square each element of the array, as opposed to multiplying the array by itself.
- .* and ./ require that the matrices have the same sizes, i.e. the same number of both rows and columns.
- Modulus is the function "mod", not an operator.
Basic Input and Output
- variable = input( 'Prompt string' ); - Issues the prompt, reads in the user's response, and stores the result in the variable
- variable = input( 'Prompt string', 's' ); - To specify a text ( string ) input instead of numbers.
- disp 'A quoted string to be displayed'
- disp ' ' % Displaying a single space creates a blank line
- disp [ 'X = ', num2str( x ) ]; % Print an array of two strings as a sentence.
- format - long, short, bank, e, +
Advanced Output Formatting ( Optional )
- fprintf( 'format string with % codes', variables )
- %f - Fixed decimal number
- %e - Exponential notation
- %g - E or F, whichever fits best
- %d - decimal integer
- width.precision - E.g. %5.2f prints a floating point number in a space of width 5, with two digits right of the decimal point.
- \n - newline
- \t - tab
- Note: When printing arrays with Matlab:
- Format specifiers are used repeatedly until all data values are printed
- Array data is accessed column-wise, not row-wise!
- fprintf example printing arrays
Creating and running scripts
- Any command that can be typed in the command window can also be run from a script file, which is just an ordinary text file containing Matlab commands.
- Matlab has a built-in text editor, launchable from an icon on the left end of the Matlab toolbar, or by clicking on an existing file in the Matlab directory window.
- Commands can either be typed into the file directly, or dragged from either the command or history windows.
- Save with a ".m" extension - Automatic when using Matlab's editor.
- ( Files must be plain text only. Do not use a word processor or other program unless you are careful to save the file as plain text. )
- Run the file by typing the name, or "run name", or run( 'name' ), or right-clicking, or from the Matlab editor.