ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:22.90KB ,
资源ID:5818114      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5818114.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(matlab中各种数据的读取.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

matlab中各种数据的读取.docx

1、matlab中各种数据的读取本技术支持指南主要处理:ASCII, binary, and MAT files.要得到MATLAB中可用来读写各种文件格式的完全函数列表,可以键入以下命令:help iofunMATLAB中有两种文件I/O程序:high level and low level.High level routines:包括现成的函数,可以用来读写特殊格式的数据,并且只需要少量的编程。Low level routines:可以更加灵活的完成相对特殊的任务,需要较多的额外编程。High level routines 包括现成的函数,可以用来读写特殊格式的数据,并且只需要少量的编程。举个

2、例子,如果你有一个包含数值和字母的文本文件(text file)想导入MATLAB,你可以调用一些low level routines自己写一个函数,或者是简单的用TEXTREAD函数。使用high level routines的关键是:文件必须是相似的(homogeneous),换句话说,文件必须有一致的格式。下面的段落描述一些high level file I/O routines并给出一些例子帮助理解概念。LOAD/SAVE主要的high level file I/O routines 是LOAD和SAVE函数。LOAD可以读MAT-file data或者用空格间隔的格式相似的ASCII

3、 data. SAVE可以将MATLAB变量写入MAT-file格式或者空格间隔的ASCII data。大多数情况下,语法相当简单。下面的例子用到数值由空格间隔的ASCII filesample_file.txt:1 5 4 16 85 43 2 6 86 8 4 32 190 7 8 7 65 9 81 2 3Example:用 LOAD and SAVE 读写数据CODE:% Load the file to the matrix, M :M = load(sample_file.txt)% Add 5 to M :M = M +5% Save M to a .mat file calle

4、d sample_file_plus5.mat:save sample_file_plus5 M% Save M to an ASCII .txt file called sample_file_plus5.txt :save sample_file_plus5.txt M -asciiUIGETFILE/UIPUTFILEUIGETFILE/UIPUTFILE是基于图形用户界面(GUI)的。会弹出对话框,列出当前目录的文件和目录,提示你选择一个文件。UIGETFILE让你选择一个文件来写(类似Windows 另存为选项?)。用UIGETFILE,可以选择已存在的文件改写,也可以输入新的文件名

5、。两个函数的返回值是所选文件名和路径。Example:用 UIGETFILE 从当前目录选择一个 M-fileCODE:% This command lists all the M-files in the current directory and% returns the name and path of the selected filefname,pname = uigetfile(*.m,Sample Dialog Box)注意: UIGETFILE 一次只能选择一个文件。UIIMPORT/IMPORTDATAUIIMPORT是一个功能强大,易于使用的基于GUI的high level

6、 routine,用于读complex data files。文件也必须是homogeneous。IMPORTDATA形成UIIMPORT的功能,不打开GUI。可以将IMPORTDATA用于函数或者脚本中,因为在函数或者脚本中基于GUI的文件导入机制并不理想。下面的例子用到包含几行文件头和文本、数值数据的文件sample_file2.txt:This is a file header.This is file is an example.col1 col2 col3 col4A 1 4 612.000B 1 4 613.000C 1 4 614.000D 1 4 615.000Example:

7、 Using IMPORTDATA to read in a file with headers, text, and numeric dataCODE:% This reads in the file sample_file2.txt and creates a% structure D that contains both data and text data.% Note the IMPORTDATA command specifies a white space% as the delimiter of the file, but IMPORTDATA can usually% det

8、ect this on its ownD = importdata(sample_file2.txt,)% 原文有误?D = importdata(sample_file2.txt)可以通过访问结构D的数据和文本域,来看结构D中的真实值,例如输入:data = D.datatext = D.textdata可以用UIIMPORT读同一个文件并得到同样的结构.注意: 对于 ASCII data, 你必须检验导入向导正确的识别了列分隔符。TEXTREAD/STRREADTEXTREAD是一个强大的动态high level routine,设计用来读ASCII格式的文本和/或数值数据文件。STRRE

9、AD除是从字符串而不是文件读以外,类似于TEXTREAD。两个函数可以用许多参数来改变其具体的工作方式,他们返回读入指定输出的数据。他们有效的提供给你一个“两全其美”的方法,因为他们可以用一个命令读入混合的ASCII和数值数据(high level routines的做法),并且你可以改变他们以匹配你特定的应用(如同low level routines做到的)。例子:CODE:Example 1: Using TEXTREAD to read in an entire file into a cell array% This command reads in the file fft.m in

10、to the cell array, filefile = textread(fft.m,%s,delimiter,n,whitespace,);CODE:Example 2: Using STRREAD to read the words in a line% This command uses the cell array created in Example 1 to% read in each word of line 28 in file to a cell array, wordswords = strread(file28,%s,delimiter,)CODE:Example 3

11、: Using TEXTREAD to read in text and numeric data from a file with headers% This command skips the 2 header lines at the top of the file% and reads in each column to the 4 specified outputsc1 c2 c3 c4 = textread(sample_file2.txt,%s %s %s %s,headerlines,2)CODE:Example 4: Using TEXTREAD to read in spe

12、cific rows of text and numeric data from a file% This command reads in rows B and C of the file. The headerlines% property is used to move down to the desired starting row and the% read operation is performed 2 timesc1 c2 c3 c4 = textread(sample_file2.txt,.%s %s %s %s,2,headerlines,4)CODE:Example 5:

13、 Using TEXTREAD to read in only the numeric data from a file containing text and numbers% This command reads in only the numeric data in the file. The% headerlines property is used to move down to the first row% of interest and the first column of text is ignored with the% *operatorc2 c3 c4 = textre

14、ad(sample_file2.txt,%*s %d %d %f,headerlines,3)DLMREAD/DLMWRITE/CSVREADDLMREAD和DLMWRITE函数能够读写分隔的ASCII data,而不是用low level routines。他们比low level routines容易使用,Low level routines用几行代码实现的功能可以用DLMREAD/DLMWRITE简化成一行。CSVREAD用来读分隔符是逗号的文件,是DLMREAD的特殊情况。当读空格和Tab分隔的电子数据表文件时,DLMREAD特别有用。以sample_file.txt为例:CODE:E

15、xample 1: Using DLMREAD to read in a file with headers, text, and numeric data% This reads in the file sample_file2.txt and creates a matrix, D,% with the numeric data this command specifies a white space as the% delimiter of the fileD = dlmread(sample_file.txt,)CODE:Example 2: Using DLMREAD to extr

16、act the first 3 columns of the last 3 rows% This reads in the first 3 columns of the last 3 rows of% the data file sample_file.txtinto the matrix, D_partial.% 读文件 sample_file.txt 前3列后3行,到矩阵D_partial.D_partial = dlmread(sample_file.txt,2 0 4 2)CODE:Example 3: Using DLMWRITE to write a comma delimited

17、 file% This creates a file called partialD.txt that consists of% the first 3 columns of the last 3 rows of data where each% element is separated by a commadlmwrite(partialD.txt,D_partial,)注意: 保证DLMREAD and DLMWRITE指定范围的指标从0开始,而不是从1开始。WK1READ/WK1WRITEWK1READ用来读Lotus123 电子数据表文件的数据;WK1WRITE用来写矩阵到Lotus1

18、23 电子数据表文件。XLSREADXLSREAD用来读Excel的数值和文本数据。三. 具体例子分析:Matlab网站用两个例子非常详尽地介绍了各个命令的基本用法,实际中,面对手头上的数据,如何选用合适的命令呢?以下结合几个示例给出一些总结,大家举一反三就可以了:1. 纯数据(列数相同):源文件:CODE:0 3866.162 2198.938 141.1401 3741.139 2208.475 141.2522 3866.200 2198.936 141.1563 3678.048 2199.191 141.2304 3685.453 2213.726 141.2615 3728.769

19、 2212.433 141.2776 3738.785 2214.381 141.2567 3728.759 2214.261 141.2288 3748.886 2214.299 141.2439 3748.935 2212.417 141.25310 3733.612 2226.653 141.23611 3733.583 2229.248 141.22312 3729.229 2229.118 141.186解答:对于这个txt文件,由于各行列数相同,故简单地使用load,importdata均可。2.字段名(中、英文字段均可)+数据:源文件:CODE:CH0 CH1 CH2 CH30.

20、000123 0.000325 0.000378 0.0005980.000986 0.000256 0.000245 0.000698解答:由于是记录的形式,因此各行列数必相同(缺少部分列时请自行在文件中补上 Inf 或 NaN),故直接使用 importdata 便可。3.注释(含有独立的数字串)+数据(列数相同):问题:这个文件有4列,但前6行是文字说明,4列数字是从第8行开始的.现在我想把这个文件的前2列和文字说明提出来组成一个新的dat文件源文件:CODE:Group 212.02.2006 LimeiSamples of datas: 50000CH0CH1CH2CH30.0001

21、230.000325 0.000378 0.0005980.0009860.000256 0.000245 0.000698目标文件:CODE:Group 2 12.02.2006 LimeiSamples of datas: 50000CH0 CH10.000123 0.0003250.000986 0.000256解答:由于注释中含有独立的数字串,且注释部分没有明显的格式, 这时候用importdata, load等高级命令直接读取会失败,用 textread, dlmwrite 等格式化命令也不太合适,因此只能使用低级命令进行读取。(当然了,可以跳过注释部分直接用高级命令读取数据,即:a

22、 b c d = textread(filename,%f %f %f %f,headerlines,4); )。一个简单的、非通用的包含注释的读取方法如下:-转 -CODE:clc;clear;fid = fopen(exp.txt, r);fid_n=fopen(ex.dat,w);while feof(fid) tline=fgetl(fid); if isempty(tline) if double(tline(1)=48 & double(tline(1)=48&double(tline(i)=57) flag=0; break; end end if flag=1 % 如果是数字行

23、,把此行数据写入文件 fprintf(fidtmp,%sn,tline); endendendfclose(fidin);fclose(fidtmp);data=textread(tmpfile);delete(tmpfile);-另外,如果要求不高,也可以使用 textread 函数跳过注释部分进行读取,不过前提是需要事先知道文件内容的结构(即哪行是数据、哪行是注释)6.各列数据的分离:源文件:CODE: 0 +47038.7 1.0509:26:07C 2 +46477.7 1.0309:28:38C 4 +44865.7 1.0409:28:48C 6 +41786.4 1.0309:28:56C 8 +39896.0 0.9709:29:03C 10 +37518.4 0.9309:29:15C 12 +35858.5 0.9209:29:30C 14 +46105.0 1.0309:30:21C 16 +46168.6 6.8909:30:30

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1