Matlab数据导入

时间:2017-04-17 06:42:34

标签: matlab import

我在文本文件中有类型的数据:

object ID: 1114
class label: 1
sign meaning: 15-fr

trajectory:
   -0.802268        0.585080
   -0.802268        0.567350
   -0.802268        0.549620
   -0.802268        0.523026
   -0.802268        0.496431
   -0.802268        0.478702
   -0.802268        0.469837
   -0.788971        0.438810
   -0.788971        0.438810
   -0.788971        0.438810
   -0.788971        0.438810
   -0.788971        0.438810
   -0.788971        0.447675
   -0.793404        0.505296
   -0.793404        0.527458
   -0.793404        0.527458
   -0.784539        0.527458
   -0.775674        0.514161
   -0.780106        0.491999
   -0.771241        0.483134
   -0.762377        0.469837
   -0.753512        0.469837
   -0.753512        0.478702
   -0.753512        0.487566
   -0.753512        0.505296
   -0.753512        0.549620
   -0.757944        0.598377
   -0.757944        0.598377
   -0.757944        0.580647
   -0.757944        0.558485
   -0.757944        0.540755
   -0.757944        0.531891
   -0.744647        0.527458
   -0.744647        0.527458
   -0.744647        0.527458
   -0.744647        0.536323
   -0.744647        0.571782
   -0.744647        0.607242
   -0.744647        0.642701
   -0.744647        0.655998
   -0.744647        0.638269
   -0.740214        0.607242
   -0.735782        0.585080
   -0.735782        0.567350
   -0.731350        0.558485
   -0.731350        0.558485
   -0.731350        0.558485
   -0.731350        0.558485
   -0.731350        0.580647
   -0.731350        0.607242
   -0.744647        0.647134
   -0.753512        0.678161
   -0.753512        0.678161
   -0.753512        0.651566
   -0.744647        0.633836
   -0.744647        0.607242
   -0.744647        0.598377
   -0.744647        0.589512
   -0.735782        0.589512
   -0.735782        0.589512
   -0.735782        0.589512
   -0.735782        0.589512
   -0.735782        0.589512
   -0.735782        0.598377
   -0.735782        0.616107
   -0.735782        0.655998
   -0.740214        0.673728
   -0.731350        0.673728
   -0.731350        0.655998
   -0.731350        0.629404
   -0.731350        0.602809
   -0.718052        0.580647
   -0.709188        0.571782
   -0.709188        0.571782
   -0.700323        0.571782
   -0.700323        0.571782
   -0.700323        0.571782
   -0.700323        0.571782
   -0.709188        0.629404
   -0.709188        0.655998
   -0.722485        0.687025
   -0.726917        0.700323
   -0.726917        0.700323

如何将其导入Matlab。我想存储所有数据,而不仅仅是数字。我在Matlab中尝试过importdata但是丢失了来自对象id和类标签的数据。上面的数据只是一个对象,我有大约2000个这样的对象。我想按顺序存储所有这些。

1 个答案:

答案 0 :(得分:0)

此脚本打开您的文件并读取标题和数据。

%Open file as text
fid=fopen('filename.txt','rt');

%First read the header information
l='';
header=struct;
l=fgetl(fid);
while ~strcmp(l,'trajectory:')
    s=regexp(l,':','split');
    s{1}=regexprep(s{1},' |-','_'); %remove spaces from header names
    if length(s)==2
        header.(s{1})=s{2};
    end
    l=fgetl(fid);
end

%then read data
data=[];
l=fgetl(fid);
while ~isnumeric(l)
    data(end+1,:)=str2num(l);
    l=fgetl(fid);
end
fclose(fid);
clear s l fid