Javascript required
Skip to content Skip to sidebar Skip to footer

Matlab Continue Text on Next Line Output

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

  1. Every program ( in any language ) should always begin with a nice large comment, explaining what the program is all about, who wrote it, when it was written, and why. It is also good to include the filename in the comment, so that it will show on printouts. More complicated programs may include a list of all the people who have worked on it, and/or a revision history documenting the important changes that have been made to the file, when, why, and by who.

In Matlab the % symbol signifies a comment, from the point of the % to the end of the line.

  1. Blank lines are ignored by Matlab. They should be used plentifully to make the program more readable to humans.
  2. The Matlab "clc" command clears out the command window, which is a good thing to do at the beginning of most scripts.

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.

  1. The "clear" command in Matlab wipes out all previously defined variables, whether they were created by this script or not. This may or may not be a good idea, depending on the situation.
  2. The "disp" command in Matlab is used to display output. In this case a simple text string, indicated by single quote marks.
  3. Blank lines can be displayed by "printing" a single space on a line by itself.
  4. The "input" command in Matlab is used to read in values from the user. It issues the prompt that is given as its first argument, reads in the user's response from the keyboard, and stores the result in the variable provided.

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.

  1. The plus sign adds two numbers and produces their sum.

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".

  1. In line 30 we want to display a combination of text strings and numerical values. In order to do this, two things must be done:
    1. The numerical values must be converted to text strings, so that all items to be displayed are of the same data type. The num2str( ) function in Matlab accomplishes this.
    2. The items must be grouped together in an array. The square brackets, [ ], indicate an array in Matlab. In this case we have an array of text strings, ( since the numbers have been converted to strings. )

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.

finnisssompoo.blogspot.com

Source: https://www.cs.uic.edu/~jbell/CourseNotes/Matlab/simpleSample.html