Path: news.mathworks.com!newsfeed-00.mathworks.com!newscon02.news.prodigy.net!prodigy.net!news-feed01.roc.ny.frontiernet.net!nntp.frontiernet.net!news02.roc.ny.POSTED!33b9410e!not-for-mail
From: Doug Schwarz <see@sig.for.address.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: eval, feval, global
References: <fsebf0$43u$1@fred.mathworks.com> <fsp7th$j9u$1@fred.mathworks.com> <fspak3$787$1@fred.mathworks.com> <5f332995-1a52-4cb2-bf08-59807891b2bf@l42g2000hsc.googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
User-Agent: MT-NewsWatcher/3.5.2 (Intel Mac OS X)
Message-ID: <see-494CA7.22033630032008@71-129-133-66.dollamir.com>
Lines: 155
X-Complaints-To: abuse-news@frontiernet.net
X-Trace: 52616e646f6d495657ef544f6079382079a311aa8c061f8fb317e7b4ffeec29d003717b49d535cd27203584a985d5b19ab49fc0b0e7ed97e413081369704466aa56dc93b6f056e17242b0e3e7c91260ce060baaf5018d2561fa9773ab906f30036dd494941a66f8d80bd47e3f19732263bc54e4d782c0a0d5821686989a69fb6
X-Abuse-Info: Please be sure to forward ALL headers so that we may process your complaint properly.
NNTP-Posting-Date: Sun, 30 Mar 2008 22:03:13 EDT
Date: Mon, 31 Mar 2008 02:03:13 GMT
Xref: news.mathworks.com comp.soft-sys.matlab:459990


In article 
<5f332995-1a52-4cb2-bf08-59807891b2bf@l42g2000hsc.googlegroups.com>,
 ImageAnalyst <imageanalyst@mailinator.com> wrote:

[snip]

> But Steven, can you pass your structure variable into a callback
> function for a button on your GUI?  As far as I know the answer is
> No.  You're limited to the 3 default callback arguments (hObject,
> etc.).  Let's say your callback function needs to use some variable
> that has been set by some other function in your program.  It's this
> situation where I use global variables because I don't know of other
> anyway to get variables into the function (i.e. ones that aren't
> retrievable from one of the other GUI controls).  If there is a way to
> get variables into a callback function without using global variables,
> let me know.
> Thanks,
> ImageAnalyst


Sure there is: the old way (using the UserData of the GUI figure) and 
the new way (using nested functions).  The following two functions do 
the same thing:


%-------------- demogui2.m --------------------------------
function demogui2(cmd)
if nargin == 0
    cmd = 'init';
end
 
switch cmd
    case 'init' % Initialize GUI application
        myname = mfilename;
        fig = figure('Position',[0 0 150 90],...
            'CreateFcn',{@movegui,'center'},...
            'NumberTitle','off',...
            'Name',myname,...
            'CloseRequestFcn',[myname,' close'],...
            'Resize','off');
        info.count = 1;
        uicontrol('Style','pushbutton',...
            'Position',[20 20 50 20],...
            'String','Down',...
            'CallBack',[myname,' dec'])
        uicontrol('Style','pushbutton',...
            'Position',[80 20 50 20],...
            'String','Up',...
            'CallBack',[myname,' inc'])
        info.editbox = uicontrol('Style','edit',...
            'Position',[45 50 60 20],...
            'String',num2str(info.count),...
            'CallBack',[myname,' edit']);
        set(fig,'UserData',info,...
            'HandleVisibility','callback')
 
    case 'dec' % Decrement counter
        fig = gcbf;
        info = get(fig,'UserData');
        info.count = info.count - 1;
        set(info.editbox,'String',num2str(info.count))
        set(fig,'UserData',info)
 
    case 'inc' % Increment counter
        fig = gcbf;
        info = get(fig,'UserData');
        info.count = info.count + 1;
        set(info.editbox,'String',num2str(info.count))
        set(fig,'UserData',info)
 
    case 'edit' % Enter value in edit box
        fig = gcbf;
        info = get(fig,'UserData');
        newcount = sscanf(get(info.editbox,'String'),'%d');
        if ~isempty(newcount)
            info.count = newcount;
        end
        set(info.editbox,'String',num2str(info.count))
        set(fig,'UserData',info)
 
    case 'close' % Close requested
        fig = gcbf;
        myname = get(fig,'Name');
        answer = questdlg(['Really close ',myname,'?'],...
            '','Yes','No','No');
        if strcmp(answer,'Yes')
            delete(fig)
        end
 
end
%----------------------------------------------------------


%-------------- demogui2_new.m ----------------------------
function demogui2_new
 
fig = figure('Position',[0 0 150 90],...
    'CreateFcn',{@movegui,'center'},...
    'NumberTitle','off',...
    'Name',mfilename,...
    'CloseRequestFcn',@close,...
    'Resize','off');
count = 1;
uicontrol('Style','pushbutton',...
    'Position',[20 20 50 20],...
    'String','Down',...
    'CallBack',@dec)
uicontrol('Style','pushbutton',...
    'Position',[80 20 50 20],...
    'String','Up',...
    'CallBack',@inc)
editbox = uicontrol('Style','edit',...
    'Position',[45 50 60 20],...
    'String',num2str(count),...
    'CallBack',@edit);
set(fig,'HandleVisibility','callback')
 
    % Decrement counter
    function dec(varargin)
        count = count - 1;
        set(editbox,'String',num2str(count))
    end
 
    % Increment counter
    function inc(varargin)
        count = count + 1;
        set(editbox,'String',num2str(count))
    end
 
    % Enter value in edit box
    function edit(varargin)
        newcount = sscanf(get(editbox,'String'),'%d');
        if ~isempty(newcount)
            count = newcount;
        end
        set(editbox,'String',num2str(count))
    end
 
    % Close requested
    function close(varargin)
        myname = get(fig,'Name');
        answer = questdlg(['Really close ',myname,'?'],...
            '','Yes','No','No');
        if strcmp(answer,'Yes')
            delete(fig)
        end
    end
 
end
%----------------------------------------------------------

-- 
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.