从长 header 的文本文件中读取数据

Read data from text file with long header

我正在尝试将以下文本文件读入 MATLAB:

Data from RamanShift_18-24-54-814.txt Node  
Date: Sun Feb 01 18:24:54 GMT 2015  
User:   
Spectrometer: QEP00413  
Autoset integration time: false 
Trigger mode: 4 
Integration Time (sec): 1.000000E0  
Scans to average: 1 
Electric dark correction enabled: true  
Nonlinearity correction enabled: false  
Boxcar width: 0 
XAxis mode: Wavelengths 
Stop averaging: false   
Number of Pixels in Spectrum: 1044  
-66.286 -2.5
-62.486 -0.5
-58.689 -0.5
-54.895 2.5
-51.104 40.5
-47.316 49.5
-43.531 52.5

我想忽略开头的文本并加载两个数字列。我试过这个:

M = dlmread('RamanShift_18-24-54-814.txt','\t',190,0);

失败并出现以下错误:

Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 2u, field 1u) ==> \n

如果我只选择第二列,通过:

M = dlmread('RamanShift_18-24-54-814.txt','\t',190,1);

那么这可行,但是 M 的所有其他元素都是零?

1113.50000000000
0
1104.50000000000
0
1094.50000000000
0
1069.50000000000
0

1) 我如何改进它以阅读两列和 2) 以更正备用零问题?

非常感谢, T

使用textscan,默认使用空格作为分隔符。它将在您的情况下完美运行:

fid = fopen('yourFileName.txt');
resCell = textscan(fid, '%s'); resCell = resCell{1};
fclose(fid);

然后您可以搜索始终出现在您的文件中的关键字(如 'Spectrum:')并阅读数字:

startInd = find(strcmpi(resCell, 'Spectrum:'), 1) + 2; % 2 is a shift that you specify that depends in the keyword you chose.

现在:

nSamples = (numel(resCell) - ind + 1)/2; % this should be an even number in your case! I guess that this change between sampling sessions...

并且:

data = reshape(str2double(resCell(startInd:end)), 2, nSamples);

最后但同样重要的是,玩得开心!

您正在寻找 importdata 函数

A = importdata('yourTextfile.txt','\t',14)

此行根据需要跳过文件的前 14 行,并使用 制表符 分隔符读取两个数据列.