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

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)

Adam Danz
Adam Danz il 3 Set 2019
Modificato: Adam Danz il 3 Set 2019
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

Your function is not useful, since I want certain data to be of the decimal type.
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.
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
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.

Accedi per commentare.

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

Everything works very weird. Your code works fine while I run it "step by step", if I remove the break points and run it normally it stops working. It's the same thing that initially happened to me.
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.
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.

Accedi per commentare.

Categorie

Scopri di più su App Building in Centro assistenza e File Exchange

Prodotti

Release

R2017b

Richiesto:

il 3 Set 2019

Community Treasure Hunt

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

Start Hunting!

Translated by