Introduction to Matlab
A Simple Sample
The following screen shot shows a simple sample in the Matlab editor. Specific lines are explained below the image:
Explanation of Specific Lines
In Matlab the % symbol signifies a comment, from the point of the % to the end of the line.
The semicolon is NOT required at the end of every Matlab statement. What it does is to suppress the echoing of the Matlab commands in the command window, so that only the desired results ( output with "disp" below ) are seen by the user. Without the semicolon, every value calculated by Matlab would be immediately displayed.
Note also that comments can be placed at the end of lines, after valid commands.
Note that variables in Matlab do not have to be declared before they are used. By default all Matlab numerical variables are ( arrays of ) doubles. In this case Matlab considers num1 to be a 1 x 1 array of doubles.
If a text string is to be read instead of a number, then a second argument, 's', is passed to input. See the online documentation for input for full details and examples.
The assignment operator, =, takes the value on the right side of the =, ( in this case the sum calculated by + ), and stores it into the variable on the left, in this case the new variable "total".
Note that many Matlab commands can be used as either simple commands or as functions, with slightly different results and/or restrictions. In this case, we have to call disp( ) as a function in order to display the array of text strings.
The ... in Matlab is used here as a continuation symbol, indicating that the lines numbered 30 and 31 should be considered by Matlab as one big long line. Note the vertical line along the right side of the editor - That line indicates 80 characters on a line, which is the maximum that you should put on one line if you want the program to print out nicely.
For CS 109, please do not write lines of Matlab code longer than 80 characters. Use ... as needed.