科学计算科学计算 (56).pdf
10.2 File Operations in MATLAB10.2 File Operations in MATLABOpening and closing filesReading and writing text filesReading and writing binary filesPositioning in a file1 1Opening and Closing Opening and Closing FilesFiles(1)The fopen functionIt is used to open a file.The syntax is as follows:fid=fopen(filename,permission)Argument fid is file identifier.When a file is opened successfully,it returns an integer.Otherwise,returns-1.Argument filename specifies the name of a file to access.Argument permission specifies the file access type.It can be specified as one of the following strings:r-Open a file for reading.w-Open a file for writing.a-Append data to the end of the file.r+-Open a file for reading and writing.(2)The fclose functionUsed to close the opened file,the syntax is:status=fclose(fid)Argument fid is the file identifier.If fid is all,all open files will be closed,except for standard files,i.e.the screen and keyboard.It returns a status of 0 when the close operation is successful.Otherwise,it returns-1.1 1Opening and Closing FilesOpening and Closing Files(1)The fscanf function and the fprintf functionthe fscanf function is used to read data from a text file,and the fprintf function is used to write data to a text file.The syntax is:A,count=fscanf(fid,fmt,size)count=fprintf(fid,fmt,A)Argument A is used to store data;count returns the number of elements that weresuccessfully read or written;fid is file identifier;fmt is the format of input and outputfields;and size is used to specify the dimension of A.2 2Reading and Writing Text FilesReading and Writing Text Files(1)The fscanf function and the fprintf functionfmtArgument fmt is specified as a string of conversion specifiers.Common conversion specifiersare as follows:%d-specifies integer fields format.%f-specifies Real numbers in decimal form.%e-specifies Real numbers in scientific notation.%c-specifies single character format.%s-specifies string format.Field width can be insert after%,specified as follows:n read n elements read Inf read all elements read m,n read a m-by-n matrix which is filled in column order.2 2Reading and Writing Text FilesReading and Writing Text Filesfid=fopen(ObserveRecord.txt,r);title=fscanf(fid,%s,6);qxsj=;for i=1:10qxsji,1=fscanf(fid,%s,1);qxsji,2=fscanf(fid,%s,1);qxsji,3=fscanf(fid,%f,1);qxsji,4=fscanf(fid,%f,1);qxsji,5=fscanf(fid,%f,1);qxsji,6=fscanf(fid,%s,1);endfclose(fid);Example 1:Contents of the file ObserveRecord.txt are shown in the figure.Read data in the first 10 lines of the file(1)The fread function and the fwrite functionThe fread function is used to read binary files.The fwrite function is used to write data to binary files.Here is the syntax:A,count=fread(fid,size,precision,skip)count=fwrite(fid,A,precision)Argument A is used to store data;count returns the number of elements read or written successfully;fid is the file identifier;precision sets the class and size of values to read or write;size specifies the dimension of A,which can be Inf,a integer or a two-element row vector;and skip is called circulation factor.If skip is specified,some data will be skipped periodically according to the skip.3 3Reading and Writing Binary FilesReading and Writing Binary Filesfid=fopen(SimulateData.dat,w);x=linspace(0,2*pi,100);y=exp(x).*sin(x);count=fwrite(fid,x;y,double);fclose(fid);Example 2:Calculate y=exsinx,where x0,2.Write x and y to the binary file SimulateData.dat.(1)The fseek functionThis function is used move to specified position in file.Here is the syntax:fseek(fid,offset,origin)Argument fid is file identifier;offset refers to the bytes offset from the starting location in the file;origin refers to the starting location in the file.origin can be specified as:cof or 0-indicates the current position of the file position indicator bof or-1-indicates the beginning of the file eof or 1-indicates the end of the file.4 4Positioning in a FilePositioning in a File(2)The ftell functionUsed to query the current position of the file position indicator,Here is the syntax:position=ftell(fid)The return value of the function is the number of bytes from the beginning of the file.If itreturns-1,that means the query is unsuccessful.4 4Positioning in a FilePositioning in a File(3)The feof functionUsed to test for end-of-file.If a previous operation set the end-of-file indicator for the file,the function returns 1.Otherwise,feof returns 0.4 4Positioning in a FilePositioning in a Filefid=fopen(SimulateData.dat,r);status=fseek(fid,-40*2*8,eof);x=;y=;while feof(fid)x=x;fread(fid,1,double);y=y;fread(fid,1,double);endplot(x,y);fclose(fid);Example 3:Read the last 40 data in the file generated in Example 2,and plot these data.