如何在matlab中读取文本文件时逐行提取数字

时间:2015-03-05 22:12:23

标签: matlab

我想从文件中提取数字,但我不知道为什么s仍为空。在阅读文本文件时,我想跳过第9行, 然后逐行阅读并在:字符后的两个空格字符之间提取一个数字。

以下是代码:

[fid, message]=fopen('info1.txt','r');
x=fread(fid,'char=>char');
%skip the 9 first lines of the file
for i=1:9
    fgetl(fid);
end
tline = fgets(fid);
s=[];
j=1;
while ischar(tline)
    disp(tline); 
    tline = fgetl(fid); %read line by line
    i=1;
    while double(tline(i))~=13
        if(tline(i)==':')
            while tline(i+1)~=' '
                s(j)=s(j)+tline(i+1);
            end
        else i=i+1;
        end
    end
    j=j+1;
end

这是文本文件。

-- Voice report for 1. Sound AXH1NAL --
Date: Fri Feb 27 13:46:32 2015

WARNING: some of the following measurements may be imprecise.
For more precision, go to "Pitch settings" and choose "Optimize for voice analysis".

Time range of SELECTION
   From 0 to 3.000020 seconds (duration: 3.000020 seconds)
Pitch:
   Median pitch: 239.912 Hz
   Mean pitch: 239.651 Hz
   Standard deviation: 2.029 Hz
   Minimum pitch: 233.314 Hz
   Maximum pitch: 243.288 Hz
Pulses:
   Number of pulses: 713
   Number of periods: 712
   Mean period: 4.172732E-3 seconds
   Standard deviation of period: 0.036479E-3 seconds
Voicing:
   Fraction of locally unvoiced frames: 0   (0 / 297)
   Number of voice breaks: 0
   Degree of voice breaks: 0   (0 seconds / 3.000020 seconds)
Jitter:
   Jitter (local): 0.218%
   Jitter (local, absolute): 9.104E-6 seconds
   Jitter (rap): 0.118%
   Jitter (ppq5): 0.134%
   Jitter (ddp): 0.353%
Shimmer:
   Shimmer (local): 1.018%
   Shimmer (local, dB): 0.089 dB
   Shimmer (apq3): 0.551%
   Shimmer (apq5): 0.684%
   Shimmer (apq11): 0.779%
   Shimmer (dda): 1.653%
Harmonicity of the voiced parts only:
   Mean autocorrelation: 0.997744
   Mean noise-to-harmonics ratio: 0.002262
   Mean harmonics-to-noise ratio: 26.891 dB

1 个答案:

答案 0 :(得分:0)

这是对您的功能的重写,但关键是使用regexp

fid = fopen('data.txt','r');
text=fread(fid,[1 Inf],'int8');
fclose(fid);
text=char(text);
text=strsplit(text,char(10)); 
text(1:9) = [];
values = regexp(text,'.*\:\s*([\d\.\+\-E]+).*$','tokens');  % <-- use ME!!
array = str2num(char(cell2mat(cell2mat(values(~cellfun('isempty',values))))));

您可以保留自己的功能,并且当您逐行遍历文件时,您可以使用相同的regexp命令来提取数字。然后,您需要使用cellfun('isempty',values)来测试是否在该行中找到了任何内容;然后,如果它不为空,则从单元格数组中提取char格式的数字,并使用str2num(values{1}{1})转换为数字格式。但是这里的代码一下子做了所有事情。

我保持代码紧凑,如果遇到麻烦可能会使调试变得有点棘手。如果你发现它不起作用:

fid = fopen('data.txt','r');
text=fread(fid,[1 Inf],'int8');
fclose(fid);
text=char(text);
text=strsplit(text,char(10)); 
text(1:9) = [];
values = regexp(text,'.*:\s*([\d\.\+\-E]+).*$','tokens');
ind =~cellfun('isempty',values);
array = cell2mat(cell2mat(values(ind)));
array = str2num(char(array))

如果要在文件中逐行循环,可以按如下方式提取数字:

  values = regexp(line,'.*:\s*([\d\.\+\-E]+).*$','tokens');
 if ~cellfun('isempty',values)
    s(j) = str2num(values{1}{1});
 end