Updating of uicontrol edit

28 vues (au cours des 30 derniers jours)
Yonatan Maor
Yonatan Maor le 15 Mai 2018
Hello everyone, I am trying to build a GUI programatically in Matlab for the first time and I've run into a problem.
I have a uicontrol with the edit style. I want to take the value from this control and send it to a callback of another uicontrol (which is a pushbutton). To do this, when creating the pushbutton, I've used the following line of code:
'UserData',struct('neededValue',editboxString.String),...
After running my program, I've noticed that this value is updated only once, when the program is run for the first time, and when I push the button the value that I receive in its callback is the default one, even if I updated the value.
Is there any way to update the value from the edit control while still using the UserData struct? I know that I can also use different methods, such as a Tag for the edit control, but if possible I would rather keep the whole program with the same style.
Thank you

Réponses (1)

Robert U
Robert U le 15 Mai 2018
Copied from
My Answer
Hi Yonatan Maor,
here a solution I gave for a different question which passes handles to (possibly external) callback functions ( Can I modify inputs for pushbutton callback, answer by Jan shows another solution possibility):
Example 1
function mytest
GUI.a = 1;
GUI.fh = figure;
GUI.h1 = uicontrol('style','Edit',...
'string','1',...
'Units','normalized',...
'Position',[0.1 0.8 0.8 0.2],...
'backgroundcolor','w',...
'Tag','EditField');
GUI.h2 = uicontrol('style','Edit',...
'string','XX',...
'Units','normalized',...
'Position',[0.1 0.1 0.8 0.2],...
'backgroundcolor','c',...
'Tag','EditField2',...
'Enable','off');
GUI.h3 = uicontrol('Style','PushButton',...
'String','Disp a2',...
'Units','normalized',...
'Position',[0.1 0.4 0.8 0.15],...
'callback',{@func_compute,GUI.h1,GUI.h2},...
'backgroundcolor',...
'r','FontSize',12);
end
function func_compute(~,~,InHandle,OutHandle)
a = str2double(InHandle.String);
b = a * a;
OutHandle.String = num2str(b);
end
The input of first Edit-Field is used to calculate the square of itself and is displayed in a result field.
Another possibility is to use "global" variables in the context of GUI (nested) functions:
Example 2
function mytest2
GUI = struct;
GUI.a = 1;
GUI.fh = figure;
GUI.h1 = uicontrol('style','Edit',...
'string','1',...
'Units','normalized',...
'Position',[0.1 0.8 0.8 0.2],...
'backgroundcolor','w',...
'Tag','EditField');
GUI.h2 = uicontrol('style','Edit',...
'string','XX',...
'Units','normalized',...
'Position',[0.1 0.1 0.8 0.2],...
'backgroundcolor','c',...
'Tag','EditField2',...
'Enable','off');
GUI.h3 = uicontrol('Style','PushButton',...
'String','Disp a2',...
'Units','normalized',...
'Position',[0.1 0.4 0.8 0.15],...
'callback',{@func_compute},...
'backgroundcolor',...
'r','FontSize',12);
function func_compute(~,~)
GUI.a;
GUI.a = str2double(GUI.h1.String);
b = GUI.a * GUI.a;
GUI.h2.String = num2str(b);
end
end
Kind regards,
Robert

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by