string strsplit:输入类型“char”的未定义函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18673969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
strsplit: undefined function for input type 'char'
提问by BajajG
I have a <20x1> cell array and each of them stores some data in the form of a string (as it appears to me!!!). I want to access each element of the cell as an individual string and split is in words.
我有一个 <20x1> 元胞数组,每个数组都以字符串的形式存储一些数据(在我看来!!!)。我想将单元格的每个元素作为一个单独的字符串访问,并且拆分是在单词中。
The cell array I have is <20x1> cell array and to access each element as a cell I am using a for loop.
我拥有的单元格数组是 <20x1> 单元格数组,为了将每个元素作为单元格访问,我使用了 for 循环。
for i=1:20
line=newline{i}
end
It shows me a all the elements within the array. Now since line is a string, I apply strsplit function to retrieve the words in the string.
它向我展示了数组中的所有元素。现在由于 line 是一个字符串,我应用 strsplit 函数来检索字符串中的单词。
for i=1:20
words(i,:)=strsplit(line)
end
This gives me an error message :
这给了我一条错误消息:
??? Undefined function or method 'strsplit' for input
arguments of type 'char'.
Error in ==> chk at 15
words=strsplit(newline{i})
can anyone explain me where I am wrong? Any help will be appreciated. Thanks in advance.
谁能解释我哪里错了?任何帮助将不胜感激。提前致谢。
回答by horchler
My guess is that you're using a version
of Matlab prior to R2013a. Despite the fact that they are generic functions and should have bee added ages ago, strsplit
and strjoin
were only added in this most recent version.
我的猜测是您在version
R2013a 之前使用的是 Matlab。尽管它们是通用函数并且应该在很久以前添加,strsplit
并且strjoin
仅在最新版本中添加。
There are several ways you can get around not having access to strsplit
if all you want to do is split a string into words. If all of your whitespaces are simple spaces you can just use strread
like this:
strsplit
如果您只想将字符串拆分为单词,则有多种方法可以解决无法访问的问题。如果你所有的空格都是简单的空格,你可以strread
像这样使用:
strread(line,'%s','delimiter',' ')
However, textscan
should be more robust:
但是,textscan
应该更健壮:
textscan(line,'%s')
Using regexp
should also be robust, but will likely be slower:
使用regexp
也应该是健壮的,但可能会更慢:
regexp(line,'\s+','split')
All of these return outputs as cell arrays of strings (your words), just like strsplit
. The output from textscan
is transposed relative to the others.
所有这些都以字符串元胞数组(你的词)的形式返回输出,就像strsplit
. 的输出textscan
相对于其他转置。