Need basic help w/ GUI Programming

4 次查看(过去 30 天)
Hi, folks! A little chagrinned to ask such a simple q, but I've been beating my head against the doc for a while now and getting (almost) nowhere.
I've built a GUI using GUIDE and need to get the user input back to the calling/creating program; I've done this before...by "cheating," i.e., by using globals, but I know this is not the way it's supposed to be done (and the guy I work w/ here, who generally knows more about all this stuff than I do, does it that way 'cause he hasn't been able to figure out the "right" way to do it either).
OK, so I have this gui consisting of a bunch of 'edit' uicontrols and a "Done" pushbutton and a "Cancel" pushbutton--I want the values in the edit controls to be passed back to the calling program when the user clicks on Done (and an empty array to be returned if s/he clicks Cancel). So far all I've been able to figure out how/what to do is to put the uiwait at the end of the GUI creation function, and the uiresume's at the end of the pushbuttons' respective Callbacks. The gui is created fine and the uiwait appears to behave properly, but when I click on either pushbutton, the callbacks aren't entered (I have breakpoints at each of their first lines and they're never reached) and I get the following error:
Error using FilterGUI
Too many input arguments.
Error in
FilterGUI>@(hObject,eventdata)FilterGUI('Done_Callback',hObject,eventdata,guidata(hObject))
(line 328)
'Callback',@(hObject,eventdata)FilterGUI('Done_Callback',hObject,eventdata,guidata(hObject)),...
Error using waitfor
Error while evaluating uicontrol Callback
Here's a pared down version of my code:
function h1 = FilterGUI()
appdata = [];
appdata.GUIDEOptions = struct(...
'active_h', [], ...
'taginfo', struct(...
'figure', 2, ...
'text', 19, ...
'uitable', 2, ...
'edit', 2, ...
'pushbutton', 3), ...
'override', 1, ...
'release', 13, ...
'resize', 'none', ...
'accessibility', 'off', ...
'mfile', 0, ...
'callbacks', 1, ...
'singleton', 1, ...
'syscolorfig', 1, ...
'blocking', 0, ...
'lastFilename', 'C:\Data\My Documents\JobRelated\MWDMS\MWDataMgmt\RT\Dev\Flights\FilterGUI.fig');
appdata.lastValidTag = 'figure1';
appdata.GUIDELayoutEditor = [];
appdata.initTags = struct(...
'handle', [], ...
'tag', 'figure1');
h1 = figure(...
'Units','characters',...
'Color',[0.925490196078431 0.913725490196078 0.847058823529412],...
'Colormap',[0 0 0.5625;0 0 0.625;0 0 0.6875;0 0 0.75;0 0 0.8125;0 0 0.875;0 0 0.9375;0 0 1;0 0.0625 1;0 0.125 1;0 0.1875 1;0 0.25 1;0 0.3125 1;0 0.375 1;0 0.4375 1;0 0.5 1;0 0.5625 1;0 0.625 1;0 0.6875 1;0 0.75 1;0 0.8125 1;0 0.875 1;0 0.9375 1;0 1 1;0.0625 1 1;0.125 1 0.9375;0.1875 1 0.875;0.25 1 0.8125;0.3125 1 0.75;0.375 1 0.6875;0.4375 1 0.625;0.5 1 0.5625;0.5625 1 0.5;0.625 1 0.4375;0.6875 1 0.375;0.75 1 0.3125;0.8125 1 0.25;0.875 1 0.1875;0.9375 1 0.125;1 1 0.0625;1 1 0;1 0.9375 0;1 0.875 0;1 0.8125 0;1 0.75 0;1 0.6875 0;1 0.625 0;1 0.5625 0;1 0.5 0;1 0.4375 0;1 0.375 0;1 0.3125 0;1 0.25 0;1 0.1875 0;1 0.125 0;1 0.0625 0;1 0 0;0.9375 0 0;0.875 0 0;0.8125 0 0;0.75 0 0;0.6875 0 0;0.625 0 0;0.5625 0 0],...
'IntegerHandle','off',...
'InvertHardcopy',get(0,'defaultfigureInvertHardcopy'),...
'MenuBar','none',...
'Name','Filter Time Constantant Selection',...
'NumberTitle','off',...
'PaperPosition',get(0,'defaultfigurePaperPosition'),...
'Position',[103.666666666667 30.5 52.8333333333333 31],...
'Resize','off',...
'HandleVisibility','callback',...
'UserData',[],...
'Tag','figure1',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );
appdata = [];
appdata.lastValidTag = 'text2';
h2 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'FontSize',11,...
'HorizontalAlignment','left',...
'Position',[1.83333333333333 27.875 22.3333333333333 1.625],...
'String','Pressure',...
'Style','text',...
'Tag','text2',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );
appdata = [];
appdata.lastValidTag = 'text11';
h11 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'FontSize',11,...
'HorizontalAlignment','left',...
'Position',[26.8333333333333 25 22.3333333333333 1.625],...
'String','0.1',...
'Style','edit',...
'Tag','text11',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );
appdata = [];
appdata.lastValidTag = 'DoneButton';
h20 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback',@(hObject,eventdata)FilterGUI('Done_Callback',hObject,eventdata,guidata(hObject)),...
'FontSize',14,...
'Position',[5.16666666666667 1.0625 15.1666666666667 2.375],...
'String','Done',...
'Tag','DoneButton',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );
%local_CreateFcn(h20, [], '', appdata);
appdata = [];
appdata.lastValidTag = 'CancelButton';
h21 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback',@(hObject,eventdata)FilterGUI('Cancel_Callback',hObject,eventdata,guidata(hObject)),...
'FontSize',14,...
'Position',[29.3333333333333 1.0625 16 2.375],...
'String','Cancel',...
'Tag','CancelButton',...
'CreateFcn', {@local_CreateFcn, blanks(0), appdata} );
%local_CreateFcn(h21, [], '', appdata);
uiwait(h1)
% --- Set application data first then calling the CreateFcn.
function local_CreateFcn(hObject, eventdata, createfcn, appdata)
if ~isempty(appdata)
names = fieldnames(appdata);
for i=1:length(names)
name = char(names(i));
setappdata(hObject, name, appdata.(name));
end
end
if ~isempty(createfcn)
if isa(createfcn,'function_handle')
createfcn(hObject, eventdata);
else
eval(createfcn);
end
end
function out = Done_Callback(varargin)
out = char(get(findall(get(hObject, 'Parent'),'Style','edit'), 'String'));
uiresume
function out = Cancel_Callback(varargin)
out = 0;
uiresume
Except for the 'CreateFcn' property-setting lines, the syntax of the pushbutton creation commands is consistent with that in my other GUI's (i.e., the ones in which I "cheated"; the commented out "local_CreateFcn" lines, which are the modus operendi in my other GUI's, were my attempt to see if that was the problem--it did not appear to be, but I've left them in just in case it strikes someone as important).
Oh, and I should also mention that the use of varargin arguments to the Callbacks, which differs from my "working" GUIs, were another (unsuccessful) attempt to "fix" the "Too many input arguments" error (thus, clearly (?), something else is complaining about receiving too many inputs).
Anyway, from my reading, I kind of gather that I should be using the variable "guidata" in some way, but it remains far from clear to me how, and obviously there's also some aspect of program flow that I'm still not getting--I wish I could use wxPython for this! :-(
Please help!
Thanks!

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-10-14
Going through some of the examples here http://www.mathworks.com/help/techdoc/creating_guis/f6-6010.html or watching Doug's video might be beneficial to you.
  2 个评论
David Goldsmith
David Goldsmith 2011-10-14
K, I'll check those out, thanks.
David Goldsmith
David Goldsmith 2011-10-14
Thank-you Fangjun, the video
http://blogs.mathworks.com/videos/2010/02/12/advanced-getting-an-output-from-a-guide-gui/
was just what the doctor ordered!
Others: for this particular issue, skip the examples and go straight to the video--it's short, succinct, and clear!

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2011-10-14
Here's your definition of FilterGUI:
function h1 = FilterGUI()
Note: No arguments listed. Here's how it's trying to be called:
FilterGUI('Done_Callback',hObject,eventdata,guidata(hObject))
Doesn't match up. Why don't you just say "View Callback" from GUIDE, and then inside the callback that it automatically makes, put a call to FilterGUI, which should be a separate function that takes no arguments, just like you already have it?
  2 个评论
David Goldsmith
David Goldsmith 2011-10-14
I'm unclear as to why I would want to call the function that creates the GUI (perhaps it's unclear that that is what FilterGUI does) in a callback for an object in the GUI. However, the first part of what you say makes sense to me, I think: are you saying that, even though they're defined outside the function, the fact that my callbacks are defined in the same m-file as the main function means that I have to access these callbacks indirectly through the main function (at least syntactically), and since callbacks always get passed at least two arguments--hObject and eventdata--the main function also has to accept at least two arguments (whether I use them therein or not)?
Image Analyst
Image Analyst 2011-10-14
OH sorry. I thought FilterGUI was some function that you wanted to call when a button was pushed rather than the name of the main GUI. Maybe you could upload both the fig and m file somewhere so I could try it.

请先登录,再进行评论。

类别

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