• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

MATLAB parse or split string

In MATLAB I need to parse or split the following string 'E4'.  I want to be able to split 'C4' and 'C3' and compare the two numbers.  How can I do this?
1 Solution
Are all your strings have the same format - one letter and one digit? In this case you can just use index to get the first letter or the digit.

string = 'E4';

string(1) will return 'E', and string(2) will return '4'. To convert string '4' to number you can use str2num or str2double function.

str2num(string(2)) will return number 4.

str2double is similar and can work with cell arrays.

If you have your strings in a cell array you can use cellfun function.

z = {'E4','C2','C3'}';
cellfun(@(x) str2num(x(2)), z)


zstring = cell2mat(z);

For more complicated parsing see documentation for functions strrep, strtok, regexp, regexprep, etc.
newtoperlpgmAuthor Commented:
Great, thank you very much!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now