Lesson 2.4

Inputs and Outputs


  • Learn what an input is in a program and how to define them in MATLAB.
  • Implement user inputs with the input() function.
  • Understand what an output is in a program.
  • Use disp() and fprintf() to output variables from MATLAB.

What Is an Input?

A programming input is some information that a piece of code needs in order to run. For example, if you write a program to calculate the volume of a cube, your program needs to know the dimensions of the cube. So the dimensions of the cube are the inputs! Functions that are build into MATLAB, which we use, also have inputs (e.g., function(input1,input2)).

Example 1: Simple Example of Program/m-file “Inputs”
%This program finds the volume of a cube %Input - Length of the side of a cube sideLength = 3; %Output - Volume of a cube cubeVolume = sideLength^3 Command Window Output
cubeVolume = 27 [Try this code yourself with Octave Online! Click Here]

Prompting the User to Input a Value

Besides the simple predefined in-program inputs (such as the one in Example 1), we can also actively ask the user for an input while the program is running using the input() function. Note, the input to the input() function will be a string. The string provided to the input() function will be the message displayed to the user in the Command Window.

Example 2: Using the input() Function to Prompt User for Value
%Asking the user for their input, which we store in the variable userName. %NOTE: User inputs using input() are sent through the Command Window userAge = input('What is your age?'); %We can see/output the user input has been stored like any other variable in the program. userAge Command Window Output
What is your age?15 **NOTE: User inputs using input() are sent through the Command Window.** userAge = 15 [Try this code yourself with Octave Online! Click Here]

What Is an Output?

In programming, an output can have several meanings, but in general it means something the code or program produces. A few examples of an output are a value output to the screen, a plot, or a file containing data. We first saw outputs in the Hello World lesson (LINK TO LESSON) where we displayed some messages to the Command Window. In the case of MATLAB’s built-in functions, we are returned some outputs by the function (e.g., output1 = function(input1,input2)). In this lesson, we will look more in depth at the commands used to accomplish outputting data.

Although we are not required to explicitly mark “this is an input/output” in our code, doing so can be a useful exercise as it helps you understand what is happening at the most basic level. That is, you give some input(s), the code performs whatever set of operations you told it, and it returns some output(s) (see Figure 1). We have seen this at work before when using functions where the general form is: [output1, output2,...] = function(input1,input2,...). If you have taken classes like physics, chemistry, dynamics, fluid mechanics, etc., you have seen “inputs” and “outputs” there as well. When given some data about the problem/question (input), apply a function or procedure to get a result/answer (output).

Figure 1: An example of how to conceptually think about inputs and outputs in code.

How to Display an Output

There are a number of ways to display outputs to the Command Window. Later in the course, we will cover how to display data via plots (LINK TO LESSON) and how to save data to a file (LINK TO LESSON). For now, we will stick to outputs to the Command Window using two common functions for displaying some values and/or text: disp() and fprintf(). The first one, disp(), displays one string or variable to the Command Window. It is that simple.

Example 3: Using the disp() Function to Print Data (continued from Example 1)
%This program finds the volume of a cube %Input - Specifying the length of the side of a symmetric cube sideLength = 3; %Code - Calculating the volume of a cube cubeVolume = sideLength^3; %Output - Displaying the calculated volume of the cube disp('The volume of the cube is:') disp(cubeVolume) Command Window Output
The volume of the cube is: 27 [Try this code yourself with Octave Online! Click Here]

The second function, fprintf(), can display multiple strings and variables to the Command Window in a variety of formats such as fixed decimal and scientific (see MathWorks documentation for a full list of possible formats). Note, fprintf() can do a lot more, but this is all we need for now.

Important Note: disp() automatically moves the output to the next line by default, while fprintf() needs you to enter a special character, \n, called “new line”, to do the same.

Example 4: Using the disp() Function to Print Data (continued from Example 1)
%Defining some arbitrary variables to display age = 42.5; %Age in years name = 'Ricky Bobby'; height = 182; %Height in centimeters %Outputting multiple variables fprintf('Hi, my name is %s, and I am %.1f years old and %g cm tall\n.',name,age,height) %NOTE: The inputs to the fprintf() function go in the order they were called in % the displayed string/text (purple part of fprintf()). Since the variable, "name", % was called first with “%s”, it is listed as the first variable at the end % of the function call. Command Window Output
Hi, my name is Ricky Bobby, and I am 42.5 years old and 182 cm tall. >> **Note: the symbol “>>” is shown here to indicate that the “carriage” moved to the next line as expected!** [Try this code yourself with Octave Online! Click Here]

Three of the most common “conversion characters” (or placeholders) for fprintf() are %g, %f, and %s as seen in Example 4. These conversion characters let fprintf() know what type of data you are giving it to output (more on this in the Data Types (LINK TO LESSON) lesson). %g and %f format numeric data in different ways, while %s is used for string/character input formats. For a full list of conversion character options for fprintf(), review the documentation page. You can add formatting spaces, alignment, positive and negative signs, special characters such as “$”, etc. using the options found there.

Generally, there is no correct choice between disp() and fprintf() to display your data to the Command Window. You can get the job done with both. However, fprintf() is often preferable given its flexibility and compactness.

Next Lesson

In the next lesson, we will introduce programming data types: another fundamental concept.