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)).
%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^3Command Window Output
cubeVolume = 27 [Try this code yourself with Octave Online! Click Here]
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.
%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. userAgeCommand 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]
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).
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.
%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.
%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.
In the next lesson, we will introduce programming data types: another fundamental concept.