<< Chapter < Page Chapter >> Page >

The num2str Function

The num2str function allows us to convert a number to a text string. Basic syntax is str = num2str(A) where variable A is converted to a text and stored in str . Let's see how it works in AcetyleneBottleInteractiveDisp.m . Remember to save the file with a different name before editing it, for example, AcetyleneBottleInteractiveDisp1.m .

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 partclc % 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.5The volume of the acetylene bottle is 0.48066 cubic meters.

The fopen And fclose Functions

The first command is used to open or create a file. The basic syntax for fopen is as follows:

fid = fopen(filename, permission)

For example, fo = fopen('output.txt', 'w'); opens or creates a new file named output.txt and sets the permission for writing. If the file already exists, it discards the existing contents.

fclose command is used to close a file. For example, if we type in fclose(fo); , we close the file that was created above.

The fprintf Function

fprintf function writes formatted data to the computer monitor or a file. This command can be used to save the results of a calculation to a file. To do this, first we create or open an output file with fopen , second we issue the fprintf command and then we close the output file with fclose .

The simplified syntax for fprintf is as follows:

fprintf=(fid, format, variable1, variable 2, ...)

Add the following lines to your .m file:

fo = fopen('output.txt', 'w'); fprintf(fo,'The radius of acetylene bottle: %g meters \n', r);fprintf(fo,'The height of cylindrical part of acetylene bottle: %g meters \n', h); fprintf(fo,'The volume of the acetylene bottle: %g cubic meters. \n', 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 partclc % 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 \n', r);fprintf(fo,'The height of cylindrical part of acetylene bottle: %g meters \n', h); fprintf(fo,'The volume of the acetylene bottle: %g cubic meters. \n', Vol_total);fclose(fo);

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, A brief introduction to engineering computation with matlab. OpenStax CNX. Nov 17, 2015 Download for free at http://legacy.cnx.org/content/col11371/1.11
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'A brief introduction to engineering computation with matlab' conversation and receive update notifications?

Ask