MATLAB Answers

I have added an edit box in a gui. I want that Neither alphabets nor special characters appear in it. Only the numbers are allowed to be appeared

3 views (last 30 days)
Muhammad Jibran
Muhammad Jibran on 30 Jan 2019
Commented: Muhammad Jibran on 30 Jan 2019
data= uicontrol (‘style’, ‘edit’, ‘string’, ‘ ‘, ‘position’, [35,30,60,15]);
&&& comment if user presses any alphabet or special character, nothing should be appeared in edit text box&&&

  0 Comments

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 30 Jan 2019
Edited: Cris LaPierre on 30 Jan 2019
Are you using App Designer or GUIDE? What version are you on?
If using App Designer, use the Edit Field (Numeric). If using GUIDE, have your callback function validate the input first. This can be done with a regular expression.
I'm not sure what behavior you want, but this will return any non-numeric character:
rmv = regexp(edit,'[^0-9]','match')
while this will return the numeric characters:
nums = regexp(edit,'\d*','match')
If you wanted to just remove any non-numeric character and leave the rest, you could do this:
function edit1_Callback(hObject, eventdata, handles)
...
edit=get(hObject,'string');
rmv = regexp(edit,'[^0-9]','match');
rmv = unique(rmv{:});
for loop = 1:length(rmv)
edit = strrep(edit,rmv(loop),'');
end
set(hObject,'String',edit);
Just note that this will remove a period in decimal numbers as well. If you do intend to keep it, update your regexp to
rmv = regexp(edit,'[^0-9\.]','match');

  3 Comments

Muhammad Jibran
Muhammad Jibran on 30 Jan 2019
im using adding the edit box just by dragging it from panel on a fig file. which we open in guide, and also have the relevent .m file for the same fig file.
pl guide me what should i do and how can restrict entry on any alphabet and special character
Cris LaPierre
Cris LaPierre on 30 Jan 2019
You can't prevent entry, but you can remove any non-numeric characters that are added. That's what the code I shared with you does.

Sign in to comment.

Sign in to answer this question.

Products


Release

R2013b

Translated by