Скачать книгу

finally clicking the SAVE button and the Close button. The lookfor command is available to help you find the MATLAB commands/functions that are related with a job you want to be done. The help command helps you know the usage of a particular command/function. You may type directly in the Command window

       >lookfor repeat or >help for

      to find the MATLAB commands in connection with ‘repeat’ or to find information about the ‘for loop’

      One of the many features of MATLAB is that it enables us to deal with the vectors/matrices in the same way as scalars. For instance, to input the matrices/vectors,

      type the following statements in the MATLAB Command window:

       >A=[1 2 3;4 5 6] A= 1 2 3 4 5 6 >B=[3;-2;1]; %put the semicolon at the end of the statement to suppress the result printout onto the screen >C=[1 -2 3 -4]

      At the end of the statement, press <Enter> key if you want to check the result of executing the statement immediately. Otherwise, type a semicolon ‘;’ before pressing <Enter> key so that the Command window will not be overloaded by a long display of results.

      MATLAB can handle two types of data files. One is the binary format mat‐files named ***.mat. This kind of files can preserve the values of more than one variable, but will be handled only in the MATLAB environment and cannot be shared with other programming environments. The other is the ASCII dat‐files named ***.txt, which can be shared with other programming environments, but preserve the values of only one variable.

      Beneath are a few sample statements for storing some data into a mat‐file in the current directory and reading the data back from the mat‐file.

       >save ABC A B C %store the values of A,B,C into the file 'ABC.mat' >clear A C %clear the memory of MATLAB about A,C >A %what is the value of A? Undefined function or variable 'A' >load ABC A C %read the values of A,C from the file 'ABC.mat' >A % the value of A A= 1 2 3 4 5 6

       >save B.txt B -ascii

      However, with the save/load commands into/from a dat‐file, the value of only one variable having the lowercase name can be saved/loaded, a scalar or a vector/matrix. Besides, nonnumeric data cannot be handled by using a dat‐file. If you save a string data into a dat‐file, its ASCII code will be saved. If a dat‐file is constructed to have a data matrix in other environments than MATLAB, every line (row) of the file must have the same number of columns. If you want to read the data from the dat‐file in MATLAB, just type the (lowercase) filename ***.txt after ‘ load’, which will also be recognized as the name of the data contained in the dat‐file.

       >load b.txt %read the value of variable b from the ascii file 'b.txt'

      At the MATLAB prompt, you can type ‘nm112’ (the filename excluding the extension name part “.m”) and <Enter> key to run the following M‐file “nm112.m” consisting of several file input(save)/output(load) statements. Then you will see the effects of the individual statements from the running results appearing on the screen.

      %nm112.m clear A=[1 2 3;4 5 6] B=[3;-2;1]; C(2)=2; C(4)=4 disp('Press any key to see the input/output through Files') save ABC A B C %save A,B & C as a MAT-file named 'ABC.mat' clear('A','C') %remove the memory about A and C load ABC A C %read MAT-file to recollect the memory about A and C save B.txt B -ascii %save B as an ASCII-file file named 'b.txt' clear B load b.txt %read ASCII-file to recollect the memory about b b x=input('Enter x:') format short e x format rat, x format long, x format short, x

       >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

Скачать книгу


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