Specify the format correctly and it will be interpreted right even without the space:
>> sscanf(a,'%E')
ans =
424.8706
-100.1130
470.2208
Question
In MATLAB if I have a string like:
a='0.424870551E+03-0.100113019E+03 0.470220758E+03';
If I use
b=str2num(a);
I get
b =324.7575 470.2208;
That is, MATLAB performed the operation for the first two numbers rather than give a 3 column vector.
Is there a possibility to convert a into a 3 column vector without using textscan
?
This might seem as a pretty easy thing; I didn't found no further information in the str2num
documentation, so that's the reason why I'm asking,
Solution
Specify the format correctly and it will be interpreted right even without the space:
>> sscanf(a,'%E')
ans =
424.8706
-100.1130
470.2208
OTHER TIPS
You need to introduce a blank space before each minus sign, except if the minus sign is in an exponent (thanks to Rafael Monteiro for noting this). That way Matlab knows they are different numbers:
a = '0.424870551E+03-0.100113019E+03 0.470220758E+03';
ind = a=='-'; %// detect positions of "-" ...
ind2 = [0 a(1:end-1)=='E'];
ind = ind & ~ind2; %// ... but not in exponents. Thanks to Rafael Monteiro
asep = repmat(' ',1,numel(a)+nnz(ind)); %// initiallize with spaces
asep((1:numel(a))+cumsum(ind)) = a; %// fill in a. Leave a blank before each "-"
b = str2num(asep);
This gives:
a =
0.424870551E+03-0.100113019E+03 0.470220758E+03
asep =
0.424870551E+03 -0.100113019E+03 0.470220758E+03
b =
424.8706 -100.1130 470.2208
The problem is there no space between the first and second numbers. Then, the function thinks you want to subtract them.
There are four ways you can do that.
For the first three, that's what you get:
str2num('0.424870551E+03-0.100113019E+03 0.470220758E+03')
str2num('0.424870551E+03 - 0.100113019E+03 0.470220758E+03')
str2num('0.424870551E+03- 0.100113019E+03 0.470220758E+03')
ans =
324.7575 470.2208
But doing like this, it will get that you want the three numbers separately:
str2num('0.424870551E+03 -0.100113019E+03 0.470220758E+03')
ans =
424.8706 -100.1130 470.2208