• 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?
Thanks
0
newtoperlpgm
Asked:
newtoperlpgm
1 Solution
 
yuk99Commented:
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)

Or

zstring = cell2mat(z);
str2num(zstring(:,2))

For more complicated parsing see documentation for functions strrep, strtok, regexp, regexprep, etc.
0
 
newtoperlpgmAuthor Commented:
Great, thank you very much!
0
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