필터 지우기
필터 지우기

importfile regexp thousands separator returns NaN when negative

조회 수: 2 (최근 30일)
Craig Cowled
Craig Cowled 2019년 7월 17일
댓글: madhan ravi 2019년 7월 17일
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*$';

채택된 답변

madhan ravi
madhan ravi 2019년 7월 17일
str2double(regexprep(numbers,',',''))
  댓글 수: 2
Craig Cowled
Craig Cowled 2019년 7월 17일
Thank you, madhan ravi. Something very similar is in the next section of code:
% Convert numeric text to numbers.
if ~invalidThousandsSeparator
numbers = textscan(char(strrep(numbers, ',', '')), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
It seems that I just need to remove the section of code that is causing the problem.
madhan ravi
madhan ravi 2019년 7월 17일
Attach your data file and illustrate what you want the desired result to be.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by