【问题标题】:How to extract numbers line by line while reading a text file in matlab如何在matlab中读取文本文件时逐行提取数字
【发布时间】:2015-05-07 11:13:31
【问题描述】:

我想从文件中提取数字,但我不知道为什么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

【问题讨论】:

  • 所以问题只在于提取数字,否则线条看起来没问题?
  • 如果您可以显示文件中的示例行会有所帮助。您没有执行类型转换,并且那里有一个不寻常的操作s(j)=s(j)+tline(i+1);
  • 好的,你能包括行终止标记吗?目前尚不清楚线路在哪里结束。我会帮你编辑一下...

标签: matlab


【解决方案1】:

这是对你的函数的重写,但关键是使用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) 测试是否在该行中找到任何内容;然后,如果它不为空,则从元胞数组中提取字符格式的数字并转换为数字格式,使用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

【讨论】:

  • @emily 好的,我将其拆分为更小的部分,也许您可​​以提供更多反馈以了解您在哪里收到错误消息
  • @emily values 是空的吗?
  • 值也是空的。
  • @emily 那么假设 text 不为空并且按照我的回答创建,那么 regexp 命令对您不起作用,但我不明白为什么,也许这是一个版本或平台的事情。你能测试一下 regexp 命令是否适用于像s = 'Minimum pitch: 233.314 Hz' 这样的简单字符串 run as regexp(s,'.*:\s*([\d\.\+\-E]+).*$','tokens')
  • 好的,那么您将获得values 的值。然后,您需要从生成的元胞数组中提取您的数字。剩下的代码就是这样做的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 2022-11-05
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
相关资源
最近更新 更多