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!

回答(2 个)

dpb
dpb 2015-12-12
编辑:dpb 2015-12-13
Use the appropriate format string in textscan to read as numeric instead of character. You don't show the file format but matching the input and using '%f' for the non-integer floating-pt fields will translate them correctly on input.

Walter Roberson
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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by