I can't erase non-numerical data from an editable text
Mostra commenti meno recenti
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
Risposte (2)
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 Commenti
Pedro Guevara
il 3 Set 2019
Adam Danz
il 3 Set 2019
Check out the updated suggestion.
BTW, we can't predict exactly what you need. We're only working with what you give us. This is why it's often helpful to include a lot of examples if inputs and expected outputs.
Pedro Guevara
il 3 Set 2019
Adam Danz
il 3 Set 2019
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
il 3 Set 2019
Modificato: Walter Roberson
il 3 Set 2019
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 Commenti
Pedro Guevara
il 3 Set 2019
Walter Roberson
il 4 Set 2019
What happens if you put in disp() statements to show where you have reached?
I speculate that your code might not have a drawnow() to give an opportunity for the display to update and callbacks to be run. A drawnow() at the end of the function would not hurt.
Ah, you are using this as a KeyPressFcn. KeyPressFcn callbacks only update the displayed value and the value you fetch from the String property after the object loses focus or the user presses return. If you are using a KeyPressFcn to do keystroke-by-keystroke editing of what the user types in, then you have to look at the event data to see what key the user has typed, and you have to keep track of them yourself for context; also you have to "manually" take care of backspace / delete by adjusting your idea of the buffer.
Pedro Guevara
il 4 Set 2019
Categorie
Scopri di più su App Building in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!