<< 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);

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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