8

I apologize if this is a newb question, but I have read the documentation here and it says nothing about having to input any command before using substring.

However, when I try to call it as follows:

substring('hello world', 2)

It gives me the error

??? Undefined function or method 'substring' for input arguments of type 'char'.

What is the correct way to invoke this substring?

merlin2011
  • 71,677
  • 44
  • 195
  • 329

3 Answers3

16

Not to detract from the OP's answer, which actually more directly adresses the question you ask, but assuming all you want to do is extract a certain number of characters from a string, MATLAB's indexing is all you need:

myString = 'Hello, world!';
mySubstring = myString(3:end)
mySubstring =

llo, world!
wakjah
  • 4,541
  • 1
  • 18
  • 23
  • 1
    I had an issue with a vector array that contained strings, but each value was cell type, I did convert this by using v = char(vector) and then I used this. Good stuff. – Juan Zamora Mar 01 '15 at 05:17
4

substring is not a MATLAB function at all, at least in MATLAB proper. There IS a substring JAVA function, but I have no idea if that is what you are asking.

>> which substring
substring is a Java method  % java.lang.String method

The above also tells you what you will need to do. Look here. (Google is your friend. Of course, you could as easily have done exactly what I just did, and gotten this answer far more quickly.)

You may also be talking about some custom code, written by some colleague of yours. In that case, talk to your friend. Very often I hear about tools that were written by someone, then left around as legacy code, unsupported. Eventually, it just disappears due to path issues when new versions of MATLAB are installed.

  • Thank you for this answer. a) I did not know about "which" function in matlab b) The matlab documentation I linked to did not say anything about it being a Java function. – merlin2011 Apr 21 '13 at 00:56
  • Your documentation does not say anything about using Java functions in matlab, but I can google that. – merlin2011 Apr 21 '13 at 00:57
  • True. The JAVA functionality in MATLAB is not always really that easy to use. In fact, if you are using the JAVA substring tool for that purpose alone, you are doing something silly, because that JAVA tool does something quite trivial. Personally, I'd suggest just writing your own substring tool, with that functionality. Then you always have it around. –  Apr 21 '13 at 01:02
  • Could you explain a) Why the deceptive documentation I linked in my question exists at all, and b) what primitives I could use for writing a substring tool in matlab? – merlin2011 Apr 21 '13 at 01:05
  • Note that I have used some specific JAVA functionality in MATLAB. But I ended up wrapping a class around it, to give that functionality ease of use. In the end, there were several thousand lines of MATLAB code wrapped around the JAVA. –  Apr 21 '13 at 01:05
  • Ah. Interestingly, that is a link to the Mupad notebook, which employs an entirely different syntax from MATLAB. It does have a substring tool in there. –  Apr 21 '13 at 01:12
0

You might actually want strsplit. This will parse char data by a given or default delimiter and return a cell array of the pieces.