MATLAB程序i,MATLAB文件I/O指南(3)高级文件I/O程序

2、高级文件I/O程序(High Level Routines)

High level routines包括现成的函数,可以用来读写特殊格式的数据,并且只需要少量的编程。

举个例子,如果你有一个包含数值和字母的文本文件(text file)想导入MATLAB,你可以调用一些low level routines自己写一个函数,或者是简单的用函数。

使用high level routines的关键是:文件必须是相似的(homogeneous),换句话说,文件必须有一致的格式。下面的段落描述一些high level file I/O routines并给出一些例子帮助理解概念。

LOAD/SAVE

主要的high level file I/O routines是和函数。LOAD可以读MAT-file data或者用空格间隔的格式相似的ASCII data.SAVE可以将MATLAB变量写入MAT-file格式或者空格间隔的ASCII data。大多数情况下,语法相当简单。下面的例子用到数值由空格间隔的:

1 5 4 16 8

5 43 2 6 8

6 8 4 32 1

90 7 8 7 6

5 9 81 2 3

Example:用LOADandSAVE读写数据

% 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 called 'sample_file_plus5.mat':

% Save M to an ASCII .txt file called 'sample_file_plus5.txt' :

save sample_file_plus5.txt M -ascii

UIGETFILE/UIPUTFILE

是基于图形用户界面(GUI)的。会弹出对话框,列出当前目录的文件和目录,提示你选择一个文件。让你选择一个文件来写(类似Windows‘另存为’选项?)。用,可以选择已存在的文件改写,也可以输入新的文件名。两个函数的返回值是所选文件名和路径。

Example:用UIGETFILE从当前目录选择一个 M-file

% This command lists all the M-files in the current directory and

% returns the name and path of the selected file

[fname,pname] = uigetfile('*.m','Sample Dialog Box')

注意:UIGETFILE一次只能选择一个文件。

UIIMPORT/IMPORTDATA

是一个功能强大,易于使用的基于GUI的high level routine,用于读complex data files。文件也必须是homogeneous。

形成的功能,不打开GUI。可以将IMPORTDATA用于函数或者脚本中,因为在函数或者脚本中基于GUI的文件导入机制并不理想。下面的例子用到包含几行文件头和文本、数值数据的文件:

This is a file header.

This is file is an example.

col1 col2 col3 col4

A14612.000

B14613.000

C14614.000

D14615.000

Example:UsingIMPORTDATAto read in a file with headers, text, and numeric data

% 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

% detect this on its own

D = importdata('sample_file2.txt','') %原文有误?

D = importdata('sample_file2.txt')

可以通过访问结构D的数据和文本域,来看结构D中的真实值,例如输入:

data = D.data

text = D.textdata

可以用UIIMPORT读同一个文件并得到同样的结构.

注意:对于ASCII data,你必须检验导入向导正确的识别了列分隔符。

TEXTREAD/STRREAD

是一个强大的动态high level routine,设计用来读ASCII格式的文本和/或数值数据文件。除是从字符串而不是文件读以外,类似于。

两个函数可以用许多参数来改变其具体的工作方式,他们返回读入指定输出的数据。他们有效的提供给你一个“两全其美”的方法,因为他们可以用一个命令读入混合的ASCII和数值数据(high level routines的做法),并且你可以改变他们以匹配你特定的应用(如同low level routines做到的)。例子:

Example 1:UsingTEXTREADto read in an entire file into a cell array

% This command reads in the file fft.m into the cell array, file

file = textread('fft.m','%s','delimiter','/n','whitespace','');

Example 2:UsingSTRREADto 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, words

words = strread(file{28},'%s','delimiter','')

Example 3:UsingTEXTREADto 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 outputs

[c

1 c

2 c

3 c4] = textread('sample_file2.txt','%s %s %s %s','headerlines',2)

Example 4:UsingTEXTREADto read in specific 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 times

[c

1 c

2 c

3 c4] = textread('sample_file2.txt',...

'%s %s %s %s',2,'headerlines',4)

Example 5:UsingTEXTREADto 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

% '*'  operator

[c

2 c

3 c4] = textread('sample_file2.txt','%*s %d %d %f','headerlines',3)

DLMREAD/DLMWRITE/CSVREAD

和函数能够读写分隔的ASCII data,而不是用low level routines。他们比low level routines容易使用,Low level routines用几行代码实现的功能可以用DLMREAD/DLMWRITE简化成一行。

用来读分隔符是逗号的文件,是的特殊情况。当读空格和Tab分隔的电子数据表文件时,特别有用。以为例:

Example 1:UsingDLMREADto 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 file

D = dlmread('sample_file.txt','')

Example 2:UsingDLMREADto extract 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.txt'into the matrix, D_partial.

%读文件'sample_file.txt'前3列后3行,到矩阵D_partial.

D_partial = dlmread('sample_file.txt','',[2 0 4 2])

Example 3:UsingDLMWRITEto write a comma delimited 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 comma

dlmwrite('partialD.txt',D_partial,',')

注意:保证DLMREADandDLMWRITE指定范围的指标从0开始,而不是从1开始。

WK1READ/WK1WRITE

用来读Lotus123电子数据表文件的数据;用来写矩阵到Lotus123电子数据表文件。

XLSREAD

用来读Excel的数值和文本数据。

下面的解决方案提供High Level Routines的附加信息:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值