MATLAB: Problem with variable not being global in creating GUI

global variablegui

I am exploring MATLAB GUI by writing code rather than GUIDE.
Attached is the code. What I want is that is my test_value text box should have access to handles.latitude once it is updated. For Checking this I set it such that when i type anything in it, it just should show me the latitude value. But it isnt.
Error being shown is
Error using simple_gui_4/test_output_Callback (line 40) Not enough input arguments.
Error while evaluating UIControl Callback
Can someone please help me?

Best Answer

function latitude_Callback(hObject,eventdata)
handles = guidata(hObject);
handles.latitude = get(hObject,'String');
display(handles.latitude);
guidata(hObject,handles)
end
function test_output_Callback(hObject,eventdata)
handles = guidata(hObject);
set(findobj('Tag','test_value'),'String', handles.latitude)
end
The fundamental problem you had is that handles is not automatically passed to functions by MATLAB. handles is a GUIDE construct, and GUIDE has to go through trouble to maintain it so that routines always get the current value.
You are using nested functions so you would have been better off avoiding the traditional handles, such as
function simple_gui_4
%define variables used in nested functions
lattitude_string = '';
%define figure and controls
f = figure('Visible','off','Position',[100,100,1000,500]);
hcity = uicontrol('Style','popupmenu',...
'String',{'Delhi','Mumbai','Visakhapatnam'},...
'Position',[150,475,100,25],...
'Callback',{@popup_menu_Callback});
hlatitude = uicontrol('Style','edit',...
'Position',[150,450,100,25],...
'Callback',{@latitude_Callback});
hlatitude_label = uicontrol('Style','edit',...
'String','Latitude',...
'Position',[50,450,100,25],...
'BackgroundColor',[1 1 1]);
htest_output = uicontrol('Style','edit',...
'String','test',...
'Tag','test_value',...
'Position',[150,50,100,25],...
'Callback',{@test_output_Callback});
% Assign a name to appear in the window title.
f.Name = 'Simple GUI';
% Move the window to the center of the screen.
movegui(f,'center')
% Make the UI visible.
f.Visible = 'on';
function latitude_Callback(hObject,eventdata)
%nested function

%set variable that was defined in outer function
latitude_string = get(hObject,'String');
disp(latitude_string);
end
function test_output_Callback(hObject,eventdata)
%nested function
%uses variables defined in outer function
set(htest_output,'String',latitude_string)
end
end
Related Question