Add the following lines to your .m file:
fo = fopen('output.txt', 'w');
fprintf(fo,'The radius of acetylene bottle: %g meters nn', r);
fprintf(fo,'The height of cylindrical part of acetylene bottle: %g meters nn', h);
fprintf(fo,'The volume of the acetylene bottle: %g cubic meters. nn', Vol_total);
fclose(fo);
Here, we first create the output.txt file that will contain the following three variables r, h and Vol_total. In the fo output file, the variables are formated with %g which automatically uses the shortest display format. You can also use %i or %d for integers and %e for scientific notation. In our script above, the \n (newline) moves the cursor to the next line.
Naming the new .m file as AcetyleneBottleInteractiveOutput.m, it 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);
fo = fopen('output.txt', 'w');
fprintf(fo,'The radius of acetylene bottle: %g meters nn', r);
fprintf(fo,'The height of cylindrical part of acetylene bottle: %g meters nn', h);
fprintf(fo,'The volume of the acetylene bottle: %g cubic meters. nn', Vol_total);
fclose(fo);
Upon running the file, the output.txt file will display the following:
The radius of acetylene bottle: 0.3 meters
The height of cylindrical part of acetylene bottle: 1.5 meters
The volume of the acetylene bottle: 0.480664 cubic meters.
- 1764 reads




