Читать книгу Applied Numerical Methods Using MATLAB - Won Y. Yang - Страница 17

1.1.3 Input/Output of Data Using Keyboard

Оглавление

The command ‘ input’ enables the user to input some data via the keyboard. For example,

>x=input('Enter x: ') Enter x: 1/3 x= 0.3333

Note that the fraction 1/3 is a nonterminating decimal number, but only four digits after the decimal point is displayed as the result of executing the above command. This is a choice of formatting in MATLAB. One may choose to display more decimal places by using the command ‘ format’, which can make a fraction show up as a fraction, as a decimal number with more digits, or even in an exponential form of a normalized number times 10 to the power of some integer. For instance:

>format rat % as a rational number >x x= 1/3 >format long % as a decimal number with 14 digits >x x= 0.33333333333333 >format long e % as a long exponential form >x x= 3.333333333333333e-001 >format hex % as a hexadecimal form as represented/stored in memory >x x= 3fd5555555555555 >format short e % as a short exponential form >x x= 3.3333e-001 >format short % back to a short form(default) >x x= 0.3333

Note that the number of displayed digits is not the actual number of significant digits of the value stored in computer memory. This point will be made clear in Section 1.2.1.

There are other ways of displaying the value of a variable and a string on the screen than typing the name of the variable. Two useful commands are ‘ disp()’ and ‘ fprintf()’. The former displays the value of a variable or a string without ‘ x=’ or ‘ ans=’; the latter displays the values of several variables in a specified format and with explanatory/cosmetic strings. For example:

>disp('The value of x='), disp(x) The value of x= 0.3333

Table 1.1 summarizes the type specifiers and special characters that are used in ‘ fprintf()’ statements.

Table 1.1 Conversion type specifiers and special characters used in fprintf() statements.

Type specifier Printing form: fprintf(‘**format string**’,variables_to_be_printed,..) Special character Meaning
%c character type \n new line
%s string type \t tab
%d decimal integer number type \b backspace
%f floating point number type \r CR return
%e decimal exponential type \f form feed
%x hexadecimal integer number %% %
%bx floating number in 16 hexadecimal digits(64 bits) '' '

Beneath is a script named “nm113.m”, which uses the command ‘ input’ so that the user could input some data via the keyboard. If we run the script, it gets a value of the temperature in Fahrenheit (F) via the keyboard from the user, converts it into the temperature in Centigrade (°C) and then prints the results with some remarks both onto the screen and into a data file named ‘nm113.txt’.

%nm113.m f=input('Input the temperature in Fahrenheit[F]:'); c=5/9*(f-32); fprintf('%5.2f(in Fahrenheit) is %5.2f(in Centigrade).\n',f,c) fid=fopen('nm113.txt','w') fprintf(fid,'%5.2f(Fahrenheit) is %5.2f(Centigrade).\n',f,c) fclose(fid)

In case you want the keyboard input to be recognized as a string, you should add the character 's' as the second input argument.

>ans=input('Answer <yes> or <no>: ','s')

Applied Numerical Methods Using MATLAB

Подняться наверх