八度-使用textscan解析非定界文本

时间:2019-03-01 15:57:17

标签: octave

我在文件中有大量数据。每行的格式为:

1个字符,整数,可选文本,可选“#”

没有空格,逗号等。我可以使用textscan来分隔这些字段吗。

一个例子

w0319

a29cde

b54863fgh

c4ijk#

b076mno

a7356pqr

d78#

b678

h765677stuvwx

谢谢

1 个答案:

答案 0 :(得分:1)

无需文本扫描。遵循以下几句话,可以为您带来良好的结果和更多的控制,并在其末尾提供漂亮的struct数组。

% Read file and split into lines as a cell array
S = fileread('myfile');
S = strsplit(S, '\n');
if isempty(S{end}); S(end) = []; end   % If there was an empty line, remove it

% Create a struct array, one struct per line
for i = 1 : length(S)
   % process mandatory character and integer
   Out(i).char = S{i}(1);              % get the first character of that line

   IntIndices = regexp( S{i}, '\d' );  % get the integer part as indices
   Out(i).int  = S{i}( IntIndices );   % note: integer returned as string
                                       %       to preserve 0-padding

   % process optional string and hash
   if IntIndices(end) == length(S{i})  % no optional string exists after integer
       Out(i).str = '';
       Out(i).hash = false;
   else
       Out(i).str  = S{i}( IntIndices(end) + 1 : end ); % get remaining string
       if strcmp( Out(i).str(end), '#' ) 
           Out(i).str(end) = [];       % remove the final hash if it exists
           Out(i).hash = true; 
       else
           Out(i).hash = false;
       end 
   end
end