Extracting number with exponential from cell array
2 次查看(过去 30 天)
显示 更早的评论
I have a 14x1 cell array, where each row contains three numbers. Some of these number are written in exponential form, and some are not. My problem is that when i try to read the values with c, it treats the E-01 as a seperate number, which makes each row of c to a 4x1 cell (for one number with exponential) instead of a 3x1. Does anyone have any suggestions for solutions..?
if true
VM = textread('newData1.vm','%s','delimiter','\n');
VM = VM(16:29,1)
c=regexp(VM,'\d+\.?\d*|-\d+\.?\d*|\.?\d+|-\.?\d+','match')
end
Thanks in advance!
0 个评论
回答(2 个)
Walter Roberson
2015-12-13
Getting a correct regexp for exponential numbers is sufficiently error prone that it is not advised.
As I look around the 'net, including in stackoverflow, most of the suggestions I see are subtly wrong in the handling of optional digits in combination with the decimal point. In particular, most of the expressions I see accept the format ".e-5" which is not valid, but gets accepted because digits before and after the "." are both optional before an exponent, but one of the two groups of numbers must be given.
The task is easier if the numbers are machine generated and can be guaranteed to have digits before the "."; if they are generated then it would help to know the generation rules. For example will they always have a leading non-zero digit before the "." except in the case of a value that is an exact 0?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!