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

2 ビュー (過去 30 日間)
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

採用された回答

Csaba
Csaba 2020 年 9 月 3 日
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

その他の回答 (1 件)

Sahithi Kanumarlapudi
Sahithi Kanumarlapudi 2020 年 8 月 27 日
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 件のコメント
Csaba
Csaba 2020 年 9 月 3 日
編集済み: Csaba 2020 年 9 月 3 日
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

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

カテゴリ

Help Center および File ExchangeWeb Services についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by