在Matlab中读取txt文件包含两个第一行中的符号

时间:2014-11-10 11:52:48

标签: matlab file file-io import

我的数据如下: MTtmax6000_N1000000_k + 0.1_k-T0.001_k-D0.1_kh1.txt

# nMT=1000000 tmax=60000 trelax=10000 k+=0.1 k-T=0.001 k-D=0.1 kh=1
#t (L-L0) L varL NGTP varNGTP Cap varCap
0  0  50090.2  2089.48     0.100257  0.100158   0.104798     0.114295
100  0.897735  50091.1  2109.92     0.099841  0.0998968   0.104373     0.114029
200  1.80163  50092  2130.83     0.099736  0.0995947   0.104204     0.113554
300  2.70513  50092.9  2151.79     0.099775  0.0997319   0.104323     0.113928
400  3.60867  50093.9  2172.17     0.099982  0.0999776   0.104546     0.114294
500  4.50984  50094.8  2192.49     0.100229  0.100263   0.104795     0.114473
600  5.40802  50095.6  2213.72     0.100149  0.100159   0.10463     0.114101
700  6.3161  50096.6  2234.2     0.099856  0.100117   0.10433     0.114139
800  7.21386  50097.5  2254.76     0.099624  0.0997151   0.104171     0.113879
900  8.11601  50098.4  2275.18     0.100183  0.100386   0.104615     0.114237
1000  9.01724  50099.3  2296.13     0.100504  0.100423   0.105058     0.114745
1100  9.92572  50100.2  2317.11     0.100368  0.10056   0.105023     0.115089
1200  10.8262  50101.1  2338.26     0.099476  0.0998665   0.103951     0.113913
1300  11.7243  50102  2359.96     0.099775  0.0997559   0.104246     0.113753
1400  12.6273  50102.9  2381.2     0.100081  0.100099   0.104571     0.11406
1500  13.5297  50103.8  2401.8     0.099702  0.0997495   0.104267     0.114045
1600  14.4281  50104.7  2422.56     0.099792  0.0999496   0.104292     0.113975
1700  15.3369  50105.6  2443.44     0.099912  0.0999296   0.104452     0.114242

当我实现接受此按摩的代码时,我尝试使用dlmread,txtscan或textread读取这些数据:

使用dlmread时出错(第139行)文件和格式字符串不匹配。无法从文件中读取数字(第1u行,第1u行)==> #nMT = 1000000 tmax = 4000 trelax = 1000 k + = 1 k-T = 0.01 k-D = 0.1 kh = 1 \ n

我希望命令读取txt文件并忽略前两行。非常感谢任何帮助。我将感激你。

     clc;
        clear all;
        close all;
        %%
        tic
        Values11 = zeros(225,6);
        K_minus_t =[0.01];
        K_minus_d = [0.1];
        %k_plus =[0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
        m=length(K_minus_t);
        r=length(K_minus_d);
        kk=0;
        ll=1;
        for l=1:r
        for j=1:m
        h=[1];
        k_plus =[1];
        K_minus_T =K_minus_t(j);
        K_minus_D = K_minus_d(l);
        sets = {k_plus, K_minus_T, K_minus_D,h};
        [x,y,z r] = ndgrid(sets{:});
        cartProd = [x(:) y(:) z(:) r(:)];
        nFiles = size(cartProd,1);
        filename{nFiles,j}=[];
        for i=1:nFiles
            %% MT_Sym_N1000000_k+1_k-T0.01_k-D0.1_kh1.txt
            filename{i,j} = ['MT_Sym_N1000000_' ...        
                'k+'  num2str(cartProd(i,1)) '_' ...
                'k-T' num2str(cartProd(i,2),'%6.3g') '_' ...
                'k-D' num2str(cartProd(i,3)) '_' ...
                'kh'  num2str(cartProd(i,4)) '' ...
                '.txt'];
            file1=dlmread(filename{i,j})
            %% line (length)
            t= file1(:,1);
            dline= file1(:,2);
            [coef_line1,s]= polyfit(t, dline, 1);
            coef_line(i,:)= coef_line1;
            v1{i}=s.R;
            v2{i}=s.df;
            v3{i}=s.normr;
            Dl(i)=sqrt (v3{i}/length(t));
    end
    end
end

1 个答案:

答案 0 :(得分:1)

使用importdata

x = importdata('file.txt',' ',2); %// ' ': col separator; 2: number of header lines
data = x.data; %// x.data is what you want

第一行为结构x提供datatextdatacolheaders字段。数字数据位于字段data中,因此您需要x.data