Could someone please tell me how to convert character array into a formatted output using Matlab?
I am expecting data like this:
CHAR (1 x 29) : 0.050822999 3.141592979 ; (1)
OR
CELL (1 x 1) or string: '0.050822999 3.141592979 ; (1)'
I am looking for output like this:
d1 = 0.050822999; %double
d2 = 3.141592979; %double
index = 1; % integer
I tried transposing and then using str2num(Str'); but, it's returning me 0x 0 double.
Any help would be appreciated.
Regards, DK
you can use regexp
to parse the string
c = { '0.050822999 3.141592979 ; (1)' };
p = regexp( c{1}, '^(\d+\.\d+)\s(\d+\.\d+)\s*;\s*\((\d+)\)$', 'tokens', 'once' ); %//parse the input string
numbers = str2mat(p); %// convert extracted strings to numerical values
Example result
ans =
0.050822999
3.141592979
1
Explaining the regexp
pattern:
^
- pattern starts at the beginning of the input string(\d+\.\d+)
- parentheses ('()'
) enclosing this sub-pattern indicates it as a single token\d+
matches one or more digits, then expecting \.
a dot (notice the \
, since .
alone in regexp acts as a wildcard) and after the dot \d+
one or more digits are expected.0.050822999
\s
expecting a single space(\d+\.\d+)
- again, expecting another decimal fraction as the second token.\s*
- expecting white space (zero or more).;
- capture the ;
in the expression, but not as a token.\s+
- expecting white space (zero or more).\(
- expecting an open parenthesis, note the \
since parentheses in regexp are used to denote tokens.(\d+)
- expecting one or more digits as the third token, only integer numbers are expected here. no decimal point.\)
- expecting a closing parenthesis.$
- pattern should reach the end of the input string.