MATLAB:要导入txt文件并删除n行后的每个标题,然后仅保留第一列

时间:2017-08-12 18:54:16

标签: matlab import header

我的txt文件如下所示,我需要导入到Matlab并仅保留'p'的值。然而,每个'时间'中的'p'存储在matlab工作空间的不同列中。每个'时间'的'p'大小为'8x1'。谢谢你的帮助。

 ASCI	I VARIABLES				
NB =	7				
Time	0				
IB	p	T	S1	S2
0	394.966	343.15	0.1	0.9
1	394.966	343.15	0.1	0.9
2	394.966	343.15	0.1	0.9
3	373.745	343.15	0.1	0.9
4	373.745	343.15	0.1	0.9
5	373.745	343.15	0.1	0.9
6	363.134	343.15	0.1	0.9
7	363.134	343.15	0.1	0.9
				
Time	0.01				
IB	p	T	S1	S2
0	394.966	343.15	0.1	0.9
1	394.966	343.15	0.1	0.9
2	394.966	343.15	0.1	0.9
3	373.745	343.15	0.1	0.9
4	373.745	343.15	0.1	0.9
5	373.745	343.15	0.1	0.9
6	363.134	343.15	0.1	0.9
7	363.134	343.15	0.1	0.9
				
Time	0.03				
IB	p	T	S1	S2
0	394.966	343.15	0.1	0.9
1	394.966	343.15	0.1	0.9
2	394.966	343.15	0.1	0.9
3	373.745	343.15	0.1	0.9
4	373.745	343.15	0.1	0.9
5	373.745	343.15	0.1	0.9
6	363.134	343.15	0.1	0.9
7	363.134	343.15	0.1	0.9
				
Time	0.07				
IB	p	T	S1	S2
0	394.966	343.15	0.1	0.9
1	394.966	343.15	0.1	0.9
2	394.966	343.15	0.1	0.9
3	373.745	343.15	0.1	0.9
4	373.745	343.15	0.1	0.9
5	373.745	343.15	0.1	0.9
6	363.134	343.15	0.1	0.9
7	363.134	343.15	0.1	0.9
				
Time	0.15				
IB	p	T	S1	S2
0	394.966	343.15	0.1	0.9
1	394.966	343.15	0.1	0.9
2	394.966	343.15	0.1	0.9
3	373.745	343.15	0.1	0.9
4	373.745	343.15	0.1	0.9
5	373.745	343.15	0.1	0.9
6	363.134	343.15	0.1	0.9
7	363.134	343.15	0.1	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

1 个答案:

答案 0 :(得分: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