MATLAB: Programatically (i.e. not using guide) accessing an edit box in another function

edit boxfunctionhandles

hi. I have create a figure and want to manually create a button and edit box on it too
%Add push button and edit boxes to analyse specific point
f1=figure;
ax1=subplot(1,1,1)
pb1 = uicontrol('Style', 'pushbutton',...
'String', {'Go'},...
'Position', [10 70 80 20],...
'Callback', @(src,evt) IntegratedIntensity( src, evt, ax1,handles ));
edx = uicontrol('Style', 'edit',...
'String', {'x pixel'},...
'Position', [10 40 40 20]);
I have connected the pushbutton to a function:
function IntegratedIntensity(source,event,ax,handles)
x=str2num(get(handles.edx,'String'));
However, when I call this function, the error indicates the edit box isn't recognised.
Reference to non-existent field 'edx'.

Best Answer

There are a few options, including
  1. Make IntegratedIntensity a nested function so it has access to the main function workspace
  2. Create your edit box before your pushbutton and pass its handle into the pushbutton callback along with ax1.
If you created a programmatic UI then 'handles' does not exist unless you explicitly add your components to a struct called handles so in both of the above options you should refer to the edit box simply as
edx
within the callback (assuming that is what you would call the variable you pass to the function in the 2nd option), rather than handles.edx
The fact you get a non-existent field error rather than a non-existent variable suggests 'handles' does exist in code you haven't shown us, but it certainly doesn't have the edit box attached to it.