Update automatically edit box when user enters values in other edit boxes

10 views (last 30 days)
Hi, I've created a script with several edit boxes and I need that they interact with themselves and update its string values only when the end user enter data in some of them. This is part of my script:
%Edit boxes where the user inserts or changes data
e0 = uicontrol('Style','edit','Position',[150 105 80 20],'String','15'); %Default value, but user can change it.
e1 = uicontrol('Style','edit','Position',[150 80 80 20]);
e2 = uicontrol('Style','edit','Position',[150 55 80 20]);
e3 = uicontrol('Style','edit','Position',[150 20 80 20];
%Edit box that needs to be updated only when the user inputs data in the previous edit boxes.
e4 = uicontrol('Style','edit','Position',[285 140 50 20],'Enable','off');
And now I want to define a function or a callback to update automatically the value of the last edit box, along these lines:
h1 = get(e1,'String'); h2 = get(e2,'String');
h3 = str2num(h1) + str2num(h2);
v1 = str2num(get(e3,'String'));
var = str2num(get(e0,'String'));
v2 = num2str(v1*171233*((288+var)-0.00198*h3)^0.5/(288-0.00198*h3)^2.628);
set(e4,'String',v2);
Any help is welcome. Thanks in advance.

Accepted Answer

Geoff Hayes
Geoff Hayes on 25 Nov 2014
isdapi - if you use the same callback function for each of your four edit boxes, and nest this function within your main code, then you can do the above. Try something like
function myGui
% create the figure
figure('Position',[ 680 809 447 169]);
% create the edit boxes
%Edit boxes where the user inserts or changes data
e0 = uicontrol('Style','edit','Position',[150 105 80 20],'String','15',...
'Callback', @updateTxtBox); %Default value, but user can change it.
e1 = uicontrol('Style','edit','Position',[150 80 80 20],'Callback',@updateTxtBox);
e2 = uicontrol('Style','edit','Position',[150 55 80 20],'Callback',@updateTxtBox);
e3 = uicontrol('Style','edit','Position',[150 20 80 20],'Callback',@updateTxtBox);
%Edit box that needs to be updated only when the user inputs data in the previous edit boxes.
e4 = uicontrol('Style','edit','Position',[285 140 50 20],'Enable','off');
% define the callback
function updateTxtBox(object,eventdata)
h1 = get(e1,'String');
h2 = get(e2,'String');
h3 = str2num(h1) + str2num(h2);
v1 = str2num(get(e3,'String'));
var = str2num(get(e0,'String'));
v2 = num2str(v1*171233*((288+var)-0.00198*h3)^0.5/(288-0.00198*h3)^2.628);
set(e4,'String',v2);
end % updateTxtBox
end % myGui
Because updateTxtBox is nested within the myGui function, it has access to the local variables (i.e. handles) of the myGui function. Note that you could/should add some code to the callback to check for non-numeric strings, and that you could create e4 as a text control rather than an edit one.
Try the above and see what happens!

More Answers (1)

isdapi
isdapi on 26 Nov 2014
Thanks for your assistance.

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!