필터 지우기
필터 지우기

Problem with variable not being global in creating GUI

조회 수: 2 (최근 30일)
RIshabh Golchha
RIshabh Golchha 2016년 1월 7일
댓글: Walter Roberson 2016년 1월 7일
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?
  댓글 수: 2
Sivakumaran Chandrasekaran
Sivakumaran Chandrasekaran 2016년 1월 7일
when i run this code, i get error on line no.28.. movegui command is not supporting structure it seems
RIshabh Golchha
RIshabh Golchha 2016년 1월 7일
It doesn't show any error for me. Maybe different versions of Matlab. Anyways you can just comment it out as it is not the primary thing

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 1월 7일
편집: Walter Roberson 2016년 1월 7일
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
  댓글 수: 2
RIshabh Golchha
RIshabh Golchha 2016년 1월 7일
The first one you gave works. But the second one is giving similar error as test_output_Callback doesn't have access to the updated latitude_string
Walter Roberson
Walter Roberson 2016년 1월 7일
It works for me once I change your f.Name and f.Visible references to the older syntax (I am using R2014a.) This is, incidentally, why Sivakumaran encountered the problem with movegui -- they must be using an older MATLAB.
set(f, 'Name', 'Simple GUI')
set(f, 'Visible', 'on')
If you are using R2014a or earlier then those changes need to be made, but for R2014b or later the syntax you used looks like it would work.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stalin Samuel
Stalin Samuel 2016년 1월 7일
편집: Stalin Samuel 2016년 1월 7일
function simple_gui_4
f = figure('Visible','on','Position',[100,100,1000,500],'Name','Simple GUI');
set(f ,'Units','Pixels','Position',get(0,'ScreenSize'))
f.hcity = uicontrol('Style','popupmenu',...
'String',{'Delhi','Mumbai','Visakhapatnam'},...
'Position',[150,475,100,25],...
'Callback',{@popup_menu_Callback});
f.hlatitude = uicontrol('Style','edit',...
'Position',[150,450,100,25],...
'Callback',{@latitude_Callback});
f.hlatitude_label = uicontrol('Style','edit',...
'String','Latitude',...
'Position',[50,450,100,25],...
'BackgroundColor',[1 1 1]);
f.htest_output = uicontrol('Style','edit',...
'String','test',...
'Tag','test_value',...
'Position',[150,50,100,25],...
'Callback',{@test_output_Callback});
%
% Make the UI visible.
% f.Visible = 'on';
function latitude_Callback(hObject,eventdata,handles)
handles.latitude = get(hObject,'String');
display(handles.latitude);
guidata(hObject,handles)
end
function test_output_Callback(hObject,eventdata,handles)
set(findobj('Tag','test_value'),'String',handles.latitude)
end
function popup_menu_Callback(hObject,eventdata,handles)
msgbox('popup executed')%replace with your code
end
end

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by