2017-08-12 29 views
0

我看起来像下面的txt文件,我需要导入到Matlab,只保留'p'的值。然而,每个'时间'中的'p'都存储在matlab工作区的不同列中。每个'时间''p'的大小将是'8x1'。感谢您的任何帮助。MATLAB:要导入txt文件并删除n行后的每个头,然后只保留第一列


ASCI \t I VARIABLES \t \t \t \t 
 
NB = \t 7 \t \t \t \t 
 
Time \t 0 \t \t \t \t 
 
IB \t p \t T \t S1 \t S2 
 
0 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
1 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
2 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
3 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
4 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
5 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
6 \t 363.134 \t 343.15 \t 0.1 \t 0.9 
 
7 \t 363.134 \t 343.15 \t 0.1 \t 0.9 
 
\t \t \t \t 
 
Time \t 0.01 \t \t \t \t 
 
IB \t p \t T \t S1 \t S2 
 
0 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
1 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
2 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
3 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
4 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
5 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
6 \t 363.134 \t 343.15 \t 0.1 \t 0.9 
 
7 \t 363.134 \t 343.15 \t 0.1 \t 0.9 
 
\t \t \t \t 
 
Time \t 0.03 \t \t \t \t 
 
IB \t p \t T \t S1 \t S2 
 
0 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
1 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
2 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
3 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
4 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
5 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
6 \t 363.134 \t 343.15 \t 0.1 \t 0.9 
 
7 \t 363.134 \t 343.15 \t 0.1 \t 0.9 
 
\t \t \t \t 
 
Time \t 0.07 \t \t \t \t 
 
IB \t p \t T \t S1 \t S2 
 
0 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
1 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
2 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
3 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
4 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
5 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
6 \t 363.134 \t 343.15 \t 0.1 \t 0.9 
 
7 \t 363.134 \t 343.15 \t 0.1 \t 0.9 
 
\t \t \t \t 
 
Time \t 0.15 \t \t \t \t 
 
IB \t p \t T \t S1 \t S2 
 
0 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
1 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
2 \t 394.966 \t 343.15 \t 0.1 \t 0.9 
 
3 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
4 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
5 \t 373.745 \t 343.15 \t 0.1 \t 0.9 
 
6 \t 363.134 \t 343.15 \t 0.1 \t 0.9 
 
7 \t 363.134 \t 343.15 \t 0.1 \t 0.9

我试图用下面的代码做这件事,但看起来不正确。

Var = 5; %number of variables 
 
NB = 8; 
 
t = Data (:,2); %obtained from another data, but the content is exactly as Time values 
 
% t = [0;0.010;0.030;0.070;0.150]; 
 
fileID = fopen('GRONINGEN.vars.txt','r'); 
 

 
for i = 1: length(t) 
 
    str = ['Time = ' num2str(t(i))]; 
 
    while (~feof(fileID)) 
 
     s = fgets(fileID); 
 
     if strfind(s, str) 
 
      break; 
 
     end 
 
    end 
 
    if(feof(fileID)) 
 
     error('End of File (EOF) found'); 
 
    end 
 

 
fgets(fileID); % skip comment string 
 
%read rest of data 
 
m=fscanf(fileID,'%e'); 
 

 
n = size(m,1)/Var; 
 

 
f=0:n-1; 
 

 
pres(i,:) = m(2+f*Var); 
 

 
fclose(fileID); 
 
end

回答

0

可以使用regexp相匹配的行。如果它同意你处理它的一些比赛,否则你移动。

fileID = fopen('GRONINGEN.vars.txt','r'); 
l = getl(fileID) 
while ~isnumeric(l) 
    if length(regexp(l,'[^\d.]'))==4 
     %we have 5 numbers in the line, so we store it 
    end 
    l = getl(fileID) 
end 
相关问题