matlab - unexpected behavior textscan matlab when skipping characters
问题描述
I'm trying to skip a set # of characters in Matlab using textscan. The documentation states that %*nc
will skip n
characters (EVEN delimiters).
I thought the following would work:
str = ' 0: 1/ 1 1| ASDFASDF | 1 | 5 ';
pat = '%f: %f/%f %f|%*17c|%f | %f';
wtf = textscan(str,pat,'MultipleDelimsAsOne',true)
which yields:
{[0]} {[1]} {[1]} {[1]} {0×1 double} {0×1 double}
This is not correct, the last entries should be 1 and 5.
By luck I happened to try capturing those characters rather than skipping ...
%Notice I removed the '*' character
pat = '%f: %f/%f %f|%17c|%f | %f';
wtf = textscan(str,pat,'MultipleDelimsAsOne',true)
which yields:
{[0]} {[1]} {[1]} {[1]} {'ASDFASDF | 1 | '} {0×1 double} {0×1 double}
This was completely unexpected, the returned string starts at the first non-space and consumes past where I had intended. Based on this I tried:
%I've increased the size and removed the bordering '|' characters
old_pat = '%f: %f/%f %f|%*17c|%f | %f';
new_pat = '%f: %f/%f %f%*19c%f | %f';
wtf = textscan(str,new_pat,'MultipleDelimsAsOne',true)
which yields my targeted result:
{[0]} {[1]} {[1]} {[1]} {[1]} {[5]}
I would have thought that after matching the literal character '|' that %*nc
would start consuming/skipping at the space, not after the first non-space. Is this behavior expected based on the documentation? What's happening here?
Note, I originally had %[^|]
in this area but I ran into problems when the text itself was '|'
or '||'
. So now I'm going to do a double pass (or maybe actually capture and then trim the whitespace) since I couldn't figure out how to adjust the textscan
call to gracefully ignore those cases.
解决方案
推荐阅读
- python - 如何在 pandas 中将 m×m 数据帧转换为 m*m×3 数据帧?
- svelte - 从 svelte 组件公开和调用方法的最佳实践
- json - 在 django 模板中渲染 JSON 而不必转义整个 JSON?
- docker - 我不能通过用户 gpadmin(不是 root)使用命令 ping
- c++ - 如何分配二维向量?
- swift - 从 [String: Any] 到 Swift 中的“Key = Value”字符串
- typescript - 打字稿:以目标字符串开头时排除属性键
- python - 将python中的名称大写
- docker - 使用 SSL 的 Nginx 子域重定向不起作用
- latex - Lilypond 到 LaTeX