您的位置:首页 > 编程语言 > MATLAB

matlab 读文件csvread textread用法实例

2014-03-17 22:31 681 查看

例1
建立文件csvlist.dat
里面数据是
   02, 04, 06, 08, 10, 12
   03, 06, 09, 12, 15, 18
   05, 10, 15, 20, 25, 30
   07, 14, 21, 28, 35, 42
   11, 22, 33, 44, 55, 66
然后将文件保存到work里,本版是保存到bin里,
在matlab里执行,
>> csvread('csvlist.dat')
运行结果:
ans =
 
     2     4     6     8    10    12
     3     6     9    12    15    18
     5    10    15    20    25    30
     7    14    21    28    35    42
11    22    33    44    55    66
 
输入
m = csvread('csvlist.dat', 2, 0)
输出
m =
 
     5    10    15    20    25    30
     7    14    21    28    35    42
11    22    33    44    55    66
 
m = csvread('csvlist.dat', 2, 0, [2,0,3,3])
 
m =
 
     5    10    15    20
     7    14    21    28
例2 textread的用法
mydata.dat 内容第一行
Sally    Level1 12.34 45 Yes
输入
[names, types, x, y, answer] = textread('mydata.dat', ...
'%s %s %f %d %s', 1)
 
 
returns
names = 
    'Sally'
types = 
    'Level1'
x =
  12.34000000000000
y =
    45
answer = 
'Yes'
 
输入[names, types, y, answer] = textread('mydata.dat', ...
'%9c %6s %*f %2d %3s', 1)
忽略含小数点的数
 
 
returns
names =
Sally    
types = 
    'Level1'
y =
    45
answer = 
'Yes'
 
第一行是Sally    Type1 12.34 45 Yes
输入
[names, typenum, x, y, answer] = textread('mydata.dat', ...
'%s Type%d %f %d %s', 1)
忽略掉字母Type
 
 
returns
names = 
    'Sally'
typenum =
    1
x =
  12.34000000000000
y =
    45
answer = 
'Yes'
文件中内容是
1,2,3,4,,6
7,8,9,,11,12
输入
data = textread('data.csv', '', 'delimiter', ',', ... 
                'emptyvalue', NaN);
用NaN填充’’空值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息