matlab 数据读取

来源:互联网 发布:朱自清背影网络 编辑:程序博客网 时间:2024/05/22 10:49

load()只能读入每一行列数相等的数据

可以使用textread函数

具体用法在下面的链接

http://blog.sina.com.cn/s/blog_9e67285801010bju.html

我的数据可以通过跳行的方法

textread

基本语法是:

    [A,B,C,…] = textread(filename,format)

    [A,B,C,…] = textread(filename,format,N)


跳行

myfiles.txt 中的内容如下:

    % this a comment

    1, 2, 3, 4

    5, 6, 7, 8

    9, 10, 11, 12

>> [data1,data2,data3,data4]=textread('myfiles.txt','%n%n%n%n','delimiter', ',','headerlines', 1);
>>  data=[data1 data2 data3 data4]

data =

     1     2     3     4
     5     6     7     8
     9    10    11    12
textread中的headerlines指明了跳过几行,1可自由设定
这里headerlines告诉textread跳过一开始的1行,1可以替换为任意你要跳过的行数。



也可以通过屏蔽字符的方式

屏蔽字符

   例4.1: 如果要忽略12.34这个浮点数。

    [names, types, y, answer] = textread('myfileli4.txt' , '%s %s %*f %d %s', 1)

    %*f 告诉textread跳过一个浮点数。
names =

    'Sally'


types =

    'Level1'


y =

    45


answer =

    'Yes'

对于iris.txt如果只想读取数据可用[data1,data2,data3,data4]=textread('Iris.txt','%f %f %f %f %*s',150,'delimiter',',');
____________________________________________________________________________________________

 例:4.2  如果要忽略Level,指读取后面的数字,

 >> [names, levelnum, x, y, answer] = textread('myfileli4.txt','%s Level%d %f %d %s', 1)

names =

    'Sally'


levelnum =

     1


x =

   12.3400


y =

    45


answer =

    'Yes'


0 0