Trouble storing a value in an edit box in a GUI using uicontrol

11 views (last 30 days)
I am creating a GUI that allows the user to edit certain parameters by typing in numbers into text boxes. A push button then allows you to apply these values to the variables in the program, or, assign particular variables with the new values. Also, I'd like these variable to be saved in the workspace if possible. In the code below, I'd like to enter a number into the Ch0_cal edit box, then assign that value to the variable Ch0cal. The code looks like this:
function LIDAR_GUI
% Keep handles available, store handle for fig in structure h.fig
% Create and hide GUI while it is being constructed
h.fig = figure('visible','off','position',[200 100 800 600],'resize','off')
% GUI title
h.GUItitle = uicontrol('style','text','string','Curvature Analysis',...
'fontsize',16,'position',[0 570 200 30])
% Labels and Parameters
h.Ch0_caltext = uicontrol('style','text','string','Channel 0 calib.',...
'position',[0 512 100 12])
h.Ch0_cal = uicontrol('style','edit','position',[100 500 50 36])
% Set parameters
h.setparams = uicontrol('style','pushbutton','position',[75 150 100 40],...
'string','Set parameters',...
'Callback',{@setparams_callback})
% Make GUI visible
set(h.fig,'visible','on','numbertitle','off','name','Curvature Analysis');
function setparams_callback(Ch0_cal,eventdata)
Ch0cal = str2double(get(Ch0_cal,'string'))
end
end
Excuse the minor formatting errors above. When I run the program, the GUI appears, I type a number into the Ch0_cal edit box, e.g. 7, press the 'Set parameters' push button, and in the command window I get
Ch0cal =
NaN
Any help to get this working? Let me know if you need more clarification on this. Thank you for your help.
EDIT: I added 'end' to the nested functions above.

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jul 2012
function setparams_callback(src,eventdata)
handles = guidata( ancestor(src, 'figure') );
Ch0_cal = handles.Ch0_cal;
Ch0cal = str2double(get(Ch0_cal,'string'))
  2 Comments
Jonathan
Jonathan on 25 Jul 2012
Something that I've found useful to think of when using GUIs is:
if you are calling an object within its own callback you use hObject to reference it
if you are calling an object from another function within the GUI you use handles.objecttag to reference it
Once I understood that GUIs made much more sense!
Josh
Josh on 25 Jul 2012
This works, thank you. Much appreciated! For the readers, don't forget to add 'end' to each callback function.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!