Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Tempoarily Disable a GUI while a function is running

Subject: Tempoarily Disable a GUI while a function is running

From: Adam

Date: 11 Sep, 2007 21:35:10

Message: 1 of 5

I have a complicated function that take awhile to run. I
have set up a GUI using GUIDE to handle different variables.
 The problem I have is the user can click the run button, or
a slider control multiple times. This calls multiple
instances of the function, with bizarre results. In a
couple cases enough functions are called to crash Matlab.

I tried making the waitbar() modal. However, you can still
sneak in 2-3 button presses. Even worse, if you click a
slider control it locks and then proceeds to repeatedly push
the slider button (calling a function instance every time)
until it reaches the max limit!!!

I'm pretty sure the following code will work:
    set(hObject, 'Enable', 'off')
    
    set(handles.lblWait, 'String', 'Thinking ...')
    pause(0.01)
    fcnTestPlot(fVal, handles); % <-- this is "long" fcn

    pause(0.01)
    set(handles.lblWait, 'String', '')
    set(hObject, 'Enable', 'on')

 It seems like a bit of a hack; i.e. is there a better way?

Subject: Re: Tempoarily Disable a GUI while a function is running

From: Steven Lord

Date: 12 Sep, 2007 04:37:16

Message: 2 of 5


"Adam " <not.my.email@mathworks.com> wrote in message
news:fc71me$ouq$1@fred.mathworks.com...
>I have a complicated function that take awhile to run. I
> have set up a GUI using GUIDE to handle different variables.
> The problem I have is the user can click the run button, or
> a slider control multiple times. This calls multiple
> instances of the function, with bizarre results. In a
> couple cases enough functions are called to crash Matlab.
>
> I tried making the waitbar() modal. However, you can still
> sneak in 2-3 button presses. Even worse, if you click a
> slider control it locks and then proceeds to repeatedly push
> the slider button (calling a function instance every time)
> until it reaches the max limit!!!

*snip*

Read this section from the documentation that discusses Callback
Interruption:

http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/creating_guis/bq61qtj-1.html

Note this is from the MATLAB 7.5 (R2007b) documentation, but I don't believe
there was any change in this functionality from MATLAB 7.4 (R2007a) to 7.5.

--
Steve Lord
slord@mathworks.com


Subject: Re: Tempoarily Disable a GUI while a function is running

From: Yair Altman

Date: 12 Sep, 2007 08:09:55

Message: 3 of 5

"Adam " <not.my.email@mathworks.com> wrote in message
<fc71me$ouq$1@fred.mathworks.com>...
> I have a complicated function that take awhile to run. I
> have set up a GUI using GUIDE to handle different variables.
> The problem I have is the user can click the run button, or
> a slider control multiple times. This calls multiple
> instances of the function, with bizarre results. In a
> couple cases enough functions are called to crash Matlab.
>
> I tried making the waitbar() modal. However, you can still
> sneak in 2-3 button presses. Even worse, if you click a
> slider control it locks and then proceeds to repeatedly push
> the slider button (calling a function instance every time)
> until it reaches the max limit!!!
>
> I'm pretty sure the following code will work:
> set(hObject, 'Enable', 'off')
>
> set(handles.lblWait, 'String', 'Thinking ...')
> pause(0.01)
> fcnTestPlot(fVal, handles); % <-- this is "long" fcn
>
> pause(0.01)
> set(handles.lblWait, 'String', '')
> set(hObject, 'Enable', 'on')
>
> It seems like a bit of a hack; i.e. is there a better way?


You might wish to use my "enable/disable figure" submission
on the File Exchange:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=15895
- it disables the entire figure, as well as all contained UI
controls and menus, in a single command. After your callback
function ends, you can call this function again to re-enable
the figure.

Yair Altman
http://ymasoftware.com

Subject: Re: Tempoarily Disable a GUI while a function is running

From: Adam

Date: 12 Sep, 2007 16:03:23

Message: 4 of 5

"Steven Lord" <slord@mathworks.com> wrote in message
<fc7qds$6pt$1@fred.mathworks.com>...
>
> "Adam " <not.my.email@mathworks.com> wrote in message
> news:fc71me$ouq$1@fred.mathworks.com...
> >I have a complicated function that take awhile to run. I
> > have set up a GUI using GUIDE to handle different variables.
> > The problem I have is the user can click the run button, or
> > a slider control multiple times. This calls multiple
> > instances of the function, with bizarre results. In a
> > couple cases enough functions are called to crash Matlab.
> >
> > I tried making the waitbar() modal. However, you can still
> > sneak in 2-3 button presses. Even worse, if you click a
> > slider control it locks and then proceeds to repeatedly push
> > the slider button (calling a function instance every time)
> > until it reaches the max limit!!!
>
> *snip*
>
> Read this section from the documentation that discusses
Callback
> Interruption:
>
>
http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/creating_guis/bq61qtj-1.html
>
> Note this is from the MATLAB 7.5 (R2007b) documentation,
but I don't believe
> there was any change in this functionality from MATLAB 7.4
(R2007a) to 7.5.
>
> --
> Steve Lord
> slord@mathworks.com

so a dialog box doesn't automatically set Interruptible to
'off' and BusyAction to 'cancel'. However, setting these
properties has no effect. Please run this code, and click
on the left, slider button.

What am I missing?

Also, callback_interrupt.m didn't come with my matlab
installation.

% --- begin code -------------------------------------------
function testCallback()
    cellName = {'BusyAction', 'Interruptible'};
    cellValue = { 'cancel', 'off'};

    hFig = figure('Position', [100 100 320 240], ...
                  cellName, cellValue);
    
    hSld = uicontrol(hFig, 'Style', 'slider', ...
                    'Position', [10 10 300 20], ...
                    'BackgroundColor', [0.9 0.9 0.9], ...
                    'Callback', @fcnRun, ...
                    'Value', 0.1, ...
                    cellName, cellValue);
                
    hTxt = uicontrol(hFig, 'Style', 'edit', ...
                    'Position', [100 100 120 20], ...
                    'BackgroundColor', 'white', ...
                    cellName, cellValue);
                
    function y = fcnRun(hObject, eventdata)
        hBox = msgbox('Please Wait', 'WindowStyle', 'modal');
        set(hBox, cellName, cellValue);

        y = zeros(1, 1e6);
        
        % do something that takes time
        for idx = 1: length(y)
            y(idx) = sin(idx * 2 * pi / 100) + randn(1, 1);
        end
        
        set(hTxt, 'String', sum(y))

        close(hBox)
    end

end
% --- end code --------------------------------

Yair, thanks for the link, but I can't use functionality
that is undocumented and unsupported.

Subject: Re: Tempoarily Disable a GUI while a function is running

From: per isakson

Date: 13 Sep, 2007 19:37:50

Message: 5 of 5

"Adam " <not.my.email@mathworks.com> wrote in message
<fc71me$ouq$1@fred.mathworks.com>...
> I have a complicated function that take awhile to run. I
> have set up a GUI using GUIDE to handle different
variables.
> The problem I have is the user can click the run button,
or
> a slider control multiple times. This calls multiple
> instances of the function, with bizarre results. In a
> couple cases enough functions are called to crash Matlab.
>
> I tried making the waitbar() modal. However, you can
still
> sneak in 2-3 button presses. Even worse, if you click a
> slider control it locks and then proceeds to repeatedly
push
> the slider button (calling a function instance every time)
> until it reaches the max limit!!!
>
> I'm pretty sure the following code will work:
> set(hObject, 'Enable', 'off')
>
> set(handles.lblWait, 'String', 'Thinking ...')
> pause(0.01)
> fcnTestPlot(fVal, handles); % <-- this is "long" fcn
>
> pause(0.01)
> set(handles.lblWait, 'String', '')
> set(hObject, 'Enable', 'on')
>
> It seems like a bit of a hack; i.e. is there a better
way?

Yes, this is bizarre. I don't understand what is going on,
but I don't think fcnRun is called multiple times.

I tried this variant, which I think works

   function y = fcnRun( hObject, eventdata ) %#ok<INUSD>
        ii = ii + 1; % ii=1; in the outer function
        fprintf( '%i', ii )
        
        set( hSld, 'Enable', 'off' )
        set( hFig, 'Pointer', 'watch' )
        drawnow
        
        y = zeros(1, 1e6);
        
        % do something that takes time
        for idx = 1: length(y)
            y(idx) = sin(idx * 2 * pi / 100) + randn(1, 1);
        end
        
        set( hTxt, 'String', sum(y) )

        set( hSld, 'Enable', 'on' )
        set( hFig, 'Pointer', 'arrow' )

    end
 

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
gui function interface Adam 11 Sep, 2007 17:40:06
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics