我在一个文件中有大量的数据。每一行都有如下格式:
1个字符,整数,可选文本,可选"#“
没有空格、逗号等。我可以使用文本扫描来分隔这些字段吗?
一个例子
w0319
a29cde
b54863fgh
c4ijk#
b076mno
a7356pqr
d78#
b678
h765677stuvwx
谢谢
发布于 2019-03-02 02:28:10
不需要文本扫描。下面的内容会给你一个好的结果和更多的控制,并在它的末尾有一个很好的结构数组。
% 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
endhttps://stackoverflow.com/questions/54948168
复制相似问题