- positive and negative signs are not digits so '-5' will return '5'
- decimal places are not digits either so '3.14' will return '314'
I can't erase non-numerical data from an editable text
조회 수: 5 (최근 30일)
이전 댓글 표시
Good afternoon. I have the following problem.
I have the following problem. I have these lines of code, which in theory should allow me to erase non-numerical data introduced to "editable text" objects. This programming works, but only when I execute the saying "step by step".
function NumElem_KeyPressFcn(hObject, eventdata, handles)
S = get(handles.NumElem,'string');
% Exclude characters, which are accepted by sscanf:
S(ismember(S, '-+eEgG')) = ' ';
% Convert to one number and back to a string:
S2 = convertCharsToStrings ( sprintf('%g', sscanf(S, '%g', 1)));
%S2 = str2double ( sprintf('%g', sscanf(S, '%g', 1)) );
set(handles.NumElem, 'String', S2);
Do you know why this problem may be due? Or some other code that allows me to eliminate non-numerical characters in real time? Thank you
댓글 수: 0
답변 (2개)
Adam Danz
2019년 9월 3일
편집: Adam Danz
2019년 9월 3일
isstrprop() can be used to identify which characters are digits within a string. Then you can removed anything that isn't a digit and will return a string of digits (or an empty string if there were no digits).
%inputStr is the input string
inputStr(isstrprop(inputStr,'digit'))
Caveats
[addendum]
If you need to match potential decimal places and potential positive/negative symbols,
regexp('this 3.14','[+-]?\d+(\.\d+)?', 'match')
[addendum II]
To control the type of data allowed in a text edit box, I recommend a function that merely identifies the input, decides whether it is acceptable or not, and if it isn't, remove the text and throw an error.
function NumElem_KeyPressFcn(hObject, eventdata, handles)
% Get the text content
S = get(handles.NumElem,'string');
% Test whether it evaluates as numeric
test = ~isnan(str2double('S')); %TRUE = ok; FALSE = fail
if ~test
% Test failed, remove bad chars
numberPart = regexp('S','[+-]?\d+(\.\d+)?', 'match'); % NOT TESTED
set(handles.NumElem,numberPart) % NOT TESTED
errordlg('Numeric input only!')
return
end
% *Not tested
댓글 수: 4
Adam Danz
2019년 9월 3일
Ok, I appended my answer again with a new suggestion based on your previous comment.
The new code I added is a sketch which means I didn't test it but it should be enough to get the idea implemented if you like the approach.
Walter Roberson
2019년 9월 3일
편집: Walter Roberson
2019년 9월 3일
S = get(handles.NumElem,'string');
S2 = strjoin( regexp(S, '(?<=(^|\D))(\d+(\.\d*)?|\.\d+)', 'match'), '');
set(handles.NumElem, 'String', S2);
Note that this code accepts multiple numbers in the field because your original code accepts multiple numbers.
Note that this code jams all of the numbers together into one long number because your original code jams all of the numbers together into one long number.
Note that this code will discard positive and negative signs and will discard exponent-building characters.
Note that this code will treat '1.23e+4' as '1.23' and '4'
Note that this code will not accept an isolated '.' as a number. It will accept positive integers, and it will accept positive integers followed by a decimal point, and it will accept positive integers followed by a decimal point followed by an integer; and it will accept a decimal point followed by an integer (that is, it does not require leading or trailing 0 after a decimal point) but it will refuse '.' by itself.
Now that I think of it you could just use
S = get(handles.NumElem,'string');
S(~ismember(S, '0123456789.')) = '';
set(handles.NumElem, 'String', S);
though this would convert 'Hello.' into '.' even though '.' by itself is not a decimal number.
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!