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 Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by