14

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.

Amro
  • 123,847
  • 25
  • 243
  • 454
BajajG
  • 2,134
  • 2
  • 25
  • 32
  • 1
    a few things to clarify, what is the output of the following commands: `s = newline{1}; whos newline s; which -all strsplit`. Also remember that the output of `strsplit` will be a cell array – Amro Sep 07 '13 at 14:26
  • The output of the command is the string in the first cell of newline:`ans = YELLOW SMALL STRETCH ADULT T` I need to extract these words in an array – BajajG Sep 07 '13 at 14:51
  • yes but thats still not enough info, you see MATLAB might print a string and a cell array containing a string very similarly, thats why I asked you to post the exact output of those commands... Also `strsplit` might be shadowed by some variable in your workspace with the same name or a completely different function/script. – Amro Sep 07 '13 at 14:59
  • I posted the exact output. I copied the output from the command window and just entered here. And there are no variables in my workspace with the same name. – BajajG Sep 07 '13 at 20:38

1 Answers1

22

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.

horchler
  • 18,384
  • 4
  • 37
  • 73
  • Yes, you are right! I am using Matlab R2010a. And thanks..strread works just the way I wanted. :) – BajajG Sep 07 '13 at 20:37
  • Matlab [recommends](http://uk.mathworks.com/help/matlab/ref/strread.html) using textscan over strsplit. – uutsav Jun 24 '15 at 11:20
  • 2
    @UtsavSinha: your link is to the documentation for **`strread`**. This question and answer are about `strsplit`, a quite different function. – horchler Jun 24 '15 at 13:08
  • Thanks @horchler for pointing it out. I wanted to say `strread` instead of `strsplit` – uutsav Jun 25 '15 at 06:23