1

我有一个包含以下指定内容的文件 Text.txt:

 !typ truck,bus,car,motor,bicycle 
 !end typ 
 !allowed car,motor,bicycle
 !end allowed

我想从“!allowed car,motor,bicycle”行中获取字符串“car,motor,bicycle”。所以我在 MATLAB 2012b 中做了这些:

io_contents = fullfile( 'Text.txt'); % open Textfile
Text = fileread( io_contents ); % read the content of io_contents
Rowbegin = strfind(Text,'!allowed'); %find the beginn of the row  in  Text
Rowend = strfind(Text,'!end allowed')-4 ;  %find the end of the row in Text
Row = Text(Rowbegin:Rowend) 
String = textscan(Row,'!allowed%s ');   
String = String{1}{1} 

它应该在 Matlab 2012b 中工作,但在 matlab 2013b 中显示此消息:

Caught "std::exception" Exception message is: invalid string position 

在第 6 行,使用的 TEXTSCAN 是。

你能告诉我原因吗,我该如何解决。是函数 textscan 的替代函数吗?非常感谢

4

1 回答 1

1

虽然我不太相信这与 2013b 版本有关,但这里有一个替代解决方案。

textscan将这一行替换为:

strrep(Row,'!allowed ','')

如果你想做更高级的事情,你可以查看正则表达式,但对于匹配字符串中的单词,这通常是最简单的方法。作为奖励,它也应该相当有效。


如果您不需要Row. 在这种情况下使用:

String = Text(Rowbegin+9:Rowend) 
于 2014-01-02T15:32:42.367 回答