Having issue after downloading tabular data from web.

조회 수: 1 (최근 30일)
Ron Aditya
Ron Aditya 2014년 9월 15일
답변: Geoff Hayes 2014년 9월 15일
After downloading a table of data from the web and converting the cells to double as I need to analyse these numbers, for some reason the is there are no digits before or after the decimal point it get converted to a whole number.
For example if "v" is the vector consisting of cells
if true
*v=
{'$ 47.21'
'$ .91'
'$ 14.11'
'$ 15'}*
% code
end
After using the following code:
if true
b1=regexp(v,'\d+(\.)?(\d+)?','match');
b(:,1)=str2double([b1{:}]);
% code
end
This is what i get:
b= [ 47.21 91 14.11 15]
instead of b= [ 47.21 0.91 14.11 15]
How do I go about fixing this, no matter what algorithm I use to fix 91 its gonna do the same to 15 as well.
Thanks,

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 9월 15일
Ron - perhaps try using regexprep instead as
b=str2double(regexprep(v,'[^0-9.-]',''))
b =
47.2100
0.9100
14.1100
15.0000
The regexprep replaces all characters that are not 0-9 or '.' or '-' with an empty string. (In regexprep(v,'[^0-9.-]',''), the ^ is the not.)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by