如何从特定单词读取来自Matlab的txt

How to read txt from Matlab from a specific word

我必须阅读这个 txt 文件:

#Acceleration force along the x y z axes (including gravity).
#timestamp(ns),x,y,z(m/s^2)
#Datetime: 05/07/2013 11:27:39
##########################################
#Activity: 12 - BSC - Back sitting chair - 10s
#Subject ID: 1
#First Name: sub1
#Last Name: sub1
#Age: 32
#Height(cm): 180
#Weight(kg): 85
#Gender: Male
##########################################


@DATA 
2318482693000, 0.89064306, -9.576807, -0.019153614 2318492108000, 0.91937345, -9.595961, -0.05746084 2318503365000,
    0.8714894, -9.595961, -0.05746084 2318512831000, 0.8523358, -9.643845, -0.038307227 2318523222000, 0.8714894, -9.634268, -0.05746084 2318533156000, 0.90979666, -9.634268, -0.019153614 2318553207000,
    0.9672575, -9.634268, -0.009576807 2318563306000, 0.93852705, -9.672575, -0.009576807 2318575332000, 0.9672575, -9.682152, -0.009576807 2318584933000, 0.9672575, -9.662998, -0.009576807 2318595971000, 0.9576807, -9.634268, 0.009576807

而且我必须在向量中的单词“@DATA”之后保存数字。 我不知道如何从某个单词(在本例中为@DATA)读取 txt 文件。

类似

fid = fopen('yourfile.extension');
while ~feof(fid)
    tline = fgetl(fid);
    disp(tline) %just for debugging purposes
    if strncmp("@DATA",tline,5)
      break
    end
end
while ~feof(fid)
%start reading your data with fscanf for example
end

永远不要忘记您的搜索引擎是您的朋友...

https://fr.mathworks.com/help/matlab/characters-and-strings.html

https://fr.mathworks.com/help/matlab/ref/feof.html

https://fr.mathworks.com/help/matlab/ref/fscanf.html

https://fr.mathworks.com/help/matlab/ref/fgetl.html

https://fr.mathworks.com/help/matlab/ref/strncmp.html

如果文件不是太大,您可以将其作为字符数组加载并使用正则表达式来解析它。如果您希望代码在内存上高效并可扩展到任何文件大小,它可能不是最好的方法,但在我看来,它非常简单。

txt = fileread(file);
valList = regexp(txt, '@DATA\n([\d\n\, \.-]+)', 'tokens');
vals = strsplit(valList{1}{1}, ', ');

这很简单,正则表达式会根据您决定如何格式化文件而改变,但这应该会让您朝着正确的方向前进。如果您将文件格式化得更漂亮,您可能可以使用更漂亮的表达式。