importfile regexp thousands separator returns NaN when negative
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Craig Cowled
am 17 Jul. 2019
Kommentiert: madhan ravi
am 17 Jul. 2019
I used "Import Data" to generate a function importfile for importing a csv file. Unfortunately, the function returns NaN for every number less than or equal to -1,000.
When I step through the generated code, I discover that the error happens in the following code:
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if numbers.contains(',')
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(numbers, thousandsRegExp, 'once'))
numbers = NaN;
invalidThousandsSeparator = true;
end
end
Let's say that:
numbers = "-1,167.67627";
Then:
regexp(numbers, thousandsRegExp, 'once')
ans =
[]
Hence, the code returns a NaN when it should be giving me a number of -1167.67627.
Compare this to the result when the number is positive:
numbers = "1,167.67627";
regexp(numbers, thousandsRegExp, 'once')
ans =
1
Obviously, the error comes from the following code, but I have no idea how to fix this 'matched regular expression':
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
0 Kommentare
Akzeptierte Antwort
madhan ravi
am 17 Jul. 2019
str2double(regexprep(numbers,',',''))
2 Kommentare
madhan ravi
am 17 Jul. 2019
Attach your data file and illustrate what you want the desired result to be.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!