简体   繁体   中英

How do I get substring to work in matlab?

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 ?

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM