You are here

Example 4.4

9 October, 2015 - 14:47

Add the following line of code to your file:

str = ['The volume of the acetylene bottle is ', num2str(Vol total), ' cubic meters.'];

Notice that the three arguments in str are separated with commas. The first argument is a simple text that is contained in ’ ’. The second argument is where the number to string conversion take place. And finally the third argument is also a simple text that completes the sentence displayed on the screen. Using semicolon at the end of the line suppresses the output. In the next line of our script, we will call str with disp(str);.

AcetyleneBottleInteractiveDisp1.m file should look like this:

        % This script computes the volume of an acetylene bottle
    % user is prompted to enter
            % a radius r for a hemispherical top
            % a height h for a cylindrical part
clc                               % Clear screen
disp('This script computes the volume of an acetylene bottle:')
disp(' ')                         % Display blank line
r=input('Enter the radius of acetylene bottle in meters ');
h=input('Enter the height of cylindrical part of acetylene bottle in meters ');
Vol_top=(2*pi*r^3)/3;             % Calculating the volume of hemispherical top [m3]
Vol_cyl=pi*r^2*h;                 % Calculating the volume of cylindrical bottom [m3]
Vol_total=Vol_top+Vol_cyl;        % Calculating the total volume of acetylene bottle [m3]
disp(' ')                         % Display blank line
str = ['The volume of the acetylene bottle is ', num2str(Vol_total), ' cubic meters.'];
disp(str);

Running the script should produce the following:

    This script computes the volume of an acetylene bottle:

Enter the radius of acetylene bottle in meters .3
Enter the height of cylindrical part of acetylene bottle in meters 1.5

The volume of the acetylene bottle is 0.48066 cubic meters.