Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

arrays - strsplit: undefined function for input type 'char'

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.

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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',' ')

However, textscan should be more robust:

textscan(line,'%s')

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...