I can't erase non-numerical data from an editable text

15 ビュー (過去 30 日間)
Pedro Guevara
Pedro Guevara 2019 年 9 月 3 日
コメント済み: Walter Roberson 2019 年 9 月 5 日
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

回答 (2 件)

Adam Danz
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
  • positive and negative signs are not digits so '-5' will return '5'
  • decimal places are not digits either so '3.14' will return '314'
[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 件のコメント
Pedro Guevara
Pedro Guevara 2019 年 9 月 3 日
Of course I can give you an example. Imagine that in my "editable text" object I enter a value of 1, with the code I designed I should enter the KeyPressFcn function of my object, and leave that value in that object. Then I make a second entry (next to the 1) of a non-numeric value such as "d", next to it would be "1d" and I should also enter the function, hoping that in real time my "editable text" object will change to only "1".
first entry = 1
second entry = d
answer after second entry (in real time) = 1
Adam Danz
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
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.
  4 件のコメント
Pedro Guevara
Pedro Guevara 2019 年 9 月 4 日
Indeed, I am using KeyPressFcn. You could give me an example or help me with this, it is not I am not sure what I should do with what you told me. Thank you.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by