首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >octave -使用textscan解析未分隔的文本

octave -使用textscan解析未分隔的文本
EN

Stack Overflow用户
提问于 2019-03-01 23:57:17
回答 1查看 41关注 0票数 0

我在一个文件中有大量的数据。每一行都有如下格式:

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

没有空格、逗号等。我可以使用文本扫描来分隔这些字段吗?

一个例子

w0319

a29cde

b54863fgh

c4ijk#

b076mno

a7356pqr

d78#

b678

h765677stuvwx

谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-03-02 02:28:10

不需要文本扫描。下面的内容会给你一个好的结果和更多的控制,并在它的末尾有一个很好的结构数组。

代码语言:javascript
复制
% 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54948168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档