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

 

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

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

举个例子,如果你有一个包含数值和字母的文本文件(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 data. SAVE可以将MATLAB变量写入MAT-file格式或者空格间隔的ASCII data。大多数情况下,语法相当简单。下面的例子用到数值由空格间隔的ASCII file sample_file.txt

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: 用 LOAD and SAVE 读写数据

% 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 sample_file_plus5 M

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

 save sample_file_plus5.txt M -ascii 

UIGETFILE/UIPUTFILE

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

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

UIIMPORT是一个功能强大,易于使用的基于GUIhigh level 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 col4

A    1   4    612.000

B    1   4    613.000

C    1   4    614.000

D    1   4    615.000

Example: Using IMPORTDATA to 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

 

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

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

Example 1: Using TEXTREAD to 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: 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, words

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

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

[c1 c2 c3 c4] = textread('sample_file2.txt','%s %s %s %s','headerlines',2) 

Example 4: Using TEXTREAD to 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 

[c1 c2 c3 c4] = textread('sample_file2.txt',... 

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

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

% '*'  operator 

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

DLMREAD/DLMWRITE/CSVREAD

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

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

 Example 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 file 

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

Example 2: Using DLMREAD to 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: Using DLMWRITE to 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,',') 

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

WK1READ/WK1WRITE

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

XLSREAD

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

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

如何使文件头中的名字s成为载变量s的名字s
如何读入ASCII分隔的有头的数据文件?
如何读逗号分隔的有数据缺失的ASCII 文?
如何使用LOAD and SAVE命令的the functional form?
如何导出a cell array containing double arrays ASCII文件格式?
当用导入向导导入二进制文件时,为何MATLAB crash?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章