strsplit:输入类型“char"的未定义函数 [英] strsplit: undefined function for input type 'char'

查看:49
本文介绍了strsplit:输入类型“char"的未定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 <20x1> 元胞数组,每个元胞数组都以字符串的形式存储一些数据(在我看来!!!).我想将单元格的每个元素作为一个单独的字符串访问,并以单词为单位进行拆分.

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> 单元格数组,为了将每个元素作为单元格访问,我使用了 for 循环.

The cell array I have is <20x1> cell array and to access each element as a cell I am using a for loop.

for i=1:20
    line=newline{i}
end

它向我展示了数组中的所有元素.现在由于 line 是一个字符串,我应用 strsplit 函数来检索字符串中的单词.

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.

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.

推荐答案

我的猜测是您正在使用 R2013a 之前的 Matlab version.尽管它们是通用函数并且应该在很久以前就被添加了,strsplitstrjoin 仅被添加到这是最新版本.

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.

如果您只想将字符串拆分为单词,则有多种方法可以避免无法访问 strsplit.如果您的所有空格都是简单的空格,您可以使用 strread 像这样:

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:

strread(line,'%s','delimiter',' ')

但是,textscan 应该是更强大:

However, textscan should be more robust:

textscan(line,'%s')

使用 regexp 也应该是健壮,但可能会更慢:

Using regexp should also be robust, but will likely be slower:

regexp(line,'\s+','split')

所有这些都以字符串元胞数组(你的话)的形式返回输出,就像 strsplit 一样.textscan 的输出相对于其他输出进行了转置.

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:输入类型“char"的未定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆