加载中…
个人资料
一片冰心
一片冰心
  • 博客等级:
  • 博客积分:0
  • 博客访问:48,238
  • 关注人气:10
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

matlab 如何批量读取数据?(如从excel表格 或一个文件内)

(2013-09-07 16:01:15)
分类: matlab
MATLAB中有两种文件I/O程序:high level and low level.
  • High level routines: 包括现成的函数,可以用来读写特殊格式的数据,并且只需要少量的编程。使用high level routines的关键是:文件必须是相似的(homogeneous),换句话说,文件必须有一致的格式。
  • Low level routines: 可以更加灵活的完成相对特殊的任务,需要较多的额外编程。
1. 从txt文件读取批量数据:
(1)简单的直接采用load函数;

  例: 当matlab当前工作空间与数据文件所在文件同在一个文件夹时,直接导入
       M=load('1.txt');
      这样数据就存到mat文件M中了。
(2)textread函数
  •   格式:
        [A,B,C,...] = textread(filename,format)
        [A,B,C,...] = textread(filename,format,N)
        [...] = textread(...,param,value,...)
  •   说明  
       TEXTREAD 是一个强大的动态high level routine,设计用来读ASCII格式的文本和/或数值数据文件。STRREAD除是从字符串而不是文件读以外,类似于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 into the cell array, file

file = 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, words
words = strread(file{28},'%s','delimiter','')

CODE:
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)


CODE:
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)


CODE:
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)
 
(3)textscan函数
        C = textscan(FID,'FORMAT') ;将数据读入细胞数组C
(4)
UIIMPORT/IMPORTDATA

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

IMPORTDATA形成UIIMPORT的功能,不打开GUI。可以将IMPORTDATA用于函数或者脚本中,因为在函数或者脚本中基于GUI的文件导入机制并不理想。
(5)
UIGETFILE/UIPUTFILE
UIGETFILE/UIPUTFILE是基于图形用户界面(GUI)的。会弹出对话框,列出当前目录的文件和目录,提示你选择一个文件。UIGETFILE让你选择一个文件来写(类似Windows ‘另存为’选项?)。用UIGETFILE,可以选择已存在的文件改写,也可以输入新的文件名。两个函数的返回值是所选文件名和路径。
Example:
用 UIGETFILE 从当前目录选择一个 M-file

CODE:

% 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 一次只能选择一个文件。
(6)
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'为例:
CODE:

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','')

CODE:

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])

CODE:

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开始。

2. 从excel文件读取数据
XLSREAD
XLSREAD用来读Excel的数值和文本数据。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有