2017-04-17 80 views
0

我有一個文本文件類型的數據:Matlab的數據導入

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個這樣的對象。我想按順序存儲所有這些。

+0

你是什麼意思「我想按順序存儲所有這些」?您可以將數據存儲在字符串的單元數組中。數據結構如下:'A {1} ='object ID:1114';''A {2} ='class label:1';'A {3} = sign含義:15-fr;'......它是否足夠將每一行存儲在一個字符串中,或​​者你是否期望更有意義的東西(例如,名稱對和值)? – Rotem

回答

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