Clear Filters
Clear Filters

Edit box 'keypressfcn' does not refresh the Edit box, unless I am debugging the callback function

3 views (last 30 days)
I have the following code (I want to avoid non numeric input in the edit box):
hfig=figure(1);
hhh = uicontrol(hfig...
,'Style','edit'...
,'Position',[10 310 150 40]...
,'HorizontalAlignment', 'right'...
,'KeyPressFcn',@keyPressCallback...
);
function keyPressCallback(source,eventdata)
switch eventdata.Key
case 'return'
box_getnumber(source);
end
end
function box_getnumber(hhh)
nummer=get(hhh,'String');
if ~isempty(nummer)
nummer=cell2mat(regexp(nummer,'[0-9e.-+]','match'));
if ~isempty(nummer)
nummer=str2double(nummer);
hhh.String=num2str(nummer,'%15.5g');
end
end
end
This code does not update the Edit box content. If I put a breakpoint anywhere in the two functions then I continue the run, the Edit box refreshes.
What is wrong in the code? (I have tried 'drawnow', it does not work!)
Csaba

Accepted Answer

Csaba
Csaba on 3 Sep 2020
I have found a workaround for the problem. If I change the focus from the edit field within the keypresscallback (i.e. to another edit field) then I come back to the original edit field the edit field refreshes.Here is the code:
hfig=figure(1);
hhh1 = uicontrol(hfig...
,'Style','edit'...
,'Position',[10 310 150 40]...
,'HorizontalAlignment', 'right'...
);
hhh2 = uicontrol(hfig...
,'Style','edit'...
,'Position',[10 210 150 40]...
,'HorizontalAlignment', 'right'...
);
hhh1.KeyPressFcn={@keyPressCallback,hhh2};
hhh2.KeyPressFcn={@keyPressCallback,hhh1};
function keyPressCallback(source,eventdata,hhh)
switch eventdata.Key
case 'return'
uicontrol(hhh);
uicontrol(source);
box_getnumber(source);
end
end
function box_getnumber(hhh)
nummer=get(hhh,'String');
if ~isempty(nummer)
nummer=cell2mat(regexp(nummer,'[0-9e.-+]','match'));
if ~isempty(nummer)
nummer=str2double(nummer);
hhh.String=num2str(nummer,'%15.5g');
end
end
end

More Answers (1)

Sahithi Kanumarlapudi
Sahithi Kanumarlapudi on 27 Aug 2020
Hi Csaba,
I have brought this issue to the notice of our developers. They will investigate the matter further.
And 'uieditfield'function could serve you as an alternative. The following code snippet is an example to create an edit field which will throw a warning on entering non-numeric data.
f = uifigure()
e = uieditfield(f,'numeric')
Hope this helps!
  1 Comment
Csaba
Csaba on 3 Sep 2020
Edited: Csaba on 3 Sep 2020
Dear Sahithi,
Your suggestion might work if I need only one numeric edit field. My example was however a simplified code, just to present the problem. Actually I have a matrix of edit fields and I want to change the focus using right/left/up/down arrows, I want to use specific characters to "freeze" some values for further execution etc. So uieditfield properties - although they are quite useful - are not good for my purpose. In this object there is no keypresscallback, so I cannot investigate the action the user did.
Nevertheless I have found a workaround, which I will present in an aswer. It also might be a little help to find what is wrong in the original code.
Edit:
I have tried your code and there is another issue with it. In most cases the problem is that a user occasionally mistypes the input, the numbers are usually good but there is a non-numeric character. uieditfield puts a warning but omits the otherwise "good" numeric characters as well. My solution extracts the numeric characters and puts back a real number into the edit field.
Csaba

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!