科学计算科学计算 (56).pdf

上传人:奉*** 文档编号:67732192 上传时间:2022-12-26 格式:PDF 页数:12 大小:584.96KB
返回 下载 相关 举报
科学计算科学计算 (56).pdf_第1页
第1页 / 共12页
科学计算科学计算 (56).pdf_第2页
第2页 / 共12页
点击查看更多>>
资源描述

《科学计算科学计算 (56).pdf》由会员分享,可在线阅读,更多相关《科学计算科学计算 (56).pdf(12页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、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=fop

2、en(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

3、 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 stand

4、ard 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 f

5、unction 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 us

6、ed 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

7、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 ord

8、er.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)

9、;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

10、(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 intege

11、r 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(

12、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 of

13、fset 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 Fil

14、ePositioning 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

15、 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.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com