ENGR 1181 MATLAB 05: Input and Output
Preparation Material

ENGR 1187 | MATLAB 05: Input and OutputPreparation Material

Learning Objectives

  1. Create a basic program that can be used over and over or given to another person to use
  2. Demonstrate proper use of the input command, which is the simplest command for interacting with the user
  3. Use fprintf, which allows the student control over how results are displayed in the command window

Topics

Read Chapter 4 of the MATLAB book before coming to class. This preparation material is provided to supplement this reading.

Students will learn a more detailed understanding of script files in MATLAB. This includes an understanding of how global variables work, and then being to learn the basics of input and output (I/O) commands. This material contains the following:

  1. Key Definitions
  2. Global Variables
  3. The input() command
  4. The disp() command
  5. The fprintf() command

1. Key Definitions

Command Window – MATLAB’s main window that opens when MATLAB is started. Script files are executed in the window, or commands can be entered directly here.

Script File – a list of MATLAB commands, called a program that is saved in a file. When executed (run), MATLAB executes the saved commands.

MATLAB Workspace – consists of the set of variables (named arrays) that are defined and stored during a MATLAB session.

2. Global Variables

The command window and script file variables are Global Variables.

  • Global variables are variables that, once created in one part of MATLAB (e.g. command window), are recognized and valid in other parts of MATLAB (e.g. script file).
  • If a variable ‘x’ is defined in the command window, then it is also recognized in the script file. The variable name and value remains in the script file until the value is re-assigned, or the variables are cleared.

There are 3 ways to input data to a script file:

  1. Define variable in script file (hardwired or hardcoded)

To change a variable:

  • Edit script file, and change the value of the variable
  • Save script file
  • Execute script file
  1. Define variable in command window (global variable)

To change a variable:

  • Type new value for variable in command window
  • Execute script file

NOTE: This only works if the script file doesn’t overwrite the variable.

  1. Define interactively with user

To change a variable:

  • Execute script file
  • Enter new value when prompted

3. The input() command

This third option is done by using the input() function in the script file, which is then executed and completed in the command window:

The script file would have the following input, where ‘text’ can be used to ask the user for an input:

The input() function will:

  • Display the string ‘text’ in the command window and wait.
  • Assign what is entered in the command window to the variable x.

For example, enter the following in the Script File:

…and the following will execute in the Command Window:

Once the user enters ‘5’ and hits ‘Enter’, the Command Window will show:

The user can also enter any valid MATLAB statement in response to the input() command. It can be a variable, a vector, a matrix, an equation, or a string.

The ‘s’ option

As you have seen, text typed into the command window in response to an input command without single quotes to indicate a string will be interpreted as a variable name. The 's' option, tells the input function to interpret whatever is typed as a string, i.e.

Thus, the user does not need to even know what a string is. Adding modifiers as a single character is common in MATLAB.

Output from a Script File

When a script file runs, output that is generated is displayed in the Command Window.

  • Output is displayed automatically if a statement does not end with a semicolon. Many times you don’t want all outputs to be displayed in the command window.
  • Output can also be displayed intentionally by using the disp()command

4. The disp() command

NOTE: disp() adds a ‘line feed’ (put cursor at start of next line) to the end of its input. Therefore, any following output appears on a new line.

5. The fprintf() command

In it’s simplest form, fprintf() looks a lot like disp(), with control characters.

Control Characters include:

\n starts a new line

\b backspace

\t horizontal tab

Unlike disp(), fprintf() does not automatically add a ‘line feed’. Therefore, the programmer must include a \n at the end of every line.

% marks where to insert a number

-is used as a flag to left justify your column

-other choices include‘+’ to add a ‘+’ or ‘-‘ before each variable, or

-0 to fill in all gaps with 0s

5is the number of spaces on the screen to reserve for the number

2is the number of digits after the decimal point

fis the number format(could also be: e,E,g,G,i)

More than one variable can be printed at a time with fprintf().

Control characters can be used to help arrange appearance.

Without ‘\n’, subsequent fprintf() commands will not start on a new line!

Tables can be printed with more control over spacing and how numbers appear

If there are more numbers to be printed than described in the 'text' portion of an fprintf() statement, then the statement will be used over and over until all numbers are displayed.

The 'text' portion of the data fprintf()command should describe the data line layout.

Arrays are displayed column by column. If an array is arranged the way you want a table to appear, as with tableYP in the previous disp example, then you need to transpose it for the fprintf() command!

1