Scripts allow user to input variable values using the input command, and display the result using
fprintf commands. The format of these commands are as follows.
Variable-name = input('Input message')
First Output Command
disp(variable-name) or disp('message statement')
Note: The command disp(variable-name) display the value of the variable, and the command
disp('message statement') display the written text message.
The next example calculates the accumulated interest when user is allowed to input variables. Here we assigned
P=1000, r=o.1, and
Second Output Command
fprintf('message statement %f more statements %f', var1, var2)
The next example shows use and flexibility of the
Consider line 6 of the above example,
fprintf('The amount %f after %f years is %f.\n', P,t,A)
The % sign marks the spot where the variable is inserted within the text. The f in %f is the conversion character which specifies the notation in which the variable value is displayed. The most common notations are f and g. With the notation g in line 6,
fprintf('The amount %g after %g years is %g.\n', P,t,A)
The Matlab shows the following result,
The amount 1000 after 2 years is 1221.4.
You can also try the other notations such as, e, E, g, G, and i.
\n is for new line (it is used to change the line). So, that the cursor
>> in the command window should appear in the next line after executing the command. You should try it by deleting the command
\n in the above example. In the end, we have three variables P, t, and A. These variables are displayed in sequence, for example, P at the first %, t at the second, and A at the third %.