欢迎来到得力文库 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
得力文库 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

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

    • 资源ID:67732192       资源大小:584.96KB        全文页数:12页
    • 资源格式: PDF        下载积分:8金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要8金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    科学计算科学计算 (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.

    注意事项

    本文(科学计算科学计算 (56).pdf)为本站会员(奉***)主动上传,得力文库 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知得力文库 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于得利文库 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档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  

    收起
    展开