Thread Subject: Nested Function Error?

Subject: Nested Function Error?

From: Bill

Date: 17 Jul, 2009 01:42:01

Message: 1 of 5

Currently, I am trying to design a GUI that has a running clock. I assumed that the best way to do it is the make a master function, "Garbage," start an infinite while loop, and embed all of the callbacks for my GUI in the infinite loop. For some reason, MATLAB doesn't like while statements that stretch across the entire program, because it says that it is an invald use of the function keyword. Any and all help would be greatly appreciated.


*********************************************************************
handles.ticFlag = 1
handles.startFlag = 1
function garbage(hObject, eventdata, handles)

while handles.ticFlag == 1

    handles.difference = toc - handles.startTime
    if handles.startFlag == 1;
        handles.timerDifference = floor(handles.difference);
        if handles.timerDifference < 10
            handles.displayedTime = horzcat('00:00:0', num2str(handles.timerDifference));
        elseif handles.TimerDifference < 60
            handles.displayedTime = horzcat('00:00:', num2str(handles.timerDifference));
        end
        set(handles.Timer, 'string', handles.displayedTime);
    end

    % --- Executes on key press with focus on figure1 and no controls selected.
    function figure1_KeyPressFcn(hObject, eventdata, handles)
    % hObject handle to figure1 (see GCBO)
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)
    end %end figure1_KeyPress

    end %end while statement

end %end function Garbage
*********************************************************************

Subject: Nested Function Error?

From: Matt Fig

Date: 17 Jul, 2009 03:46:01

Message: 2 of 5

It is true that you cannot define this kind of function in a while loop.

>>help function

Subject: Nested Function Error?

From: Shanmugam Kannappan

Date: 17 Jul, 2009 05:07:01

Message: 3 of 5

"Bill " <millerwd@notes.udayton.edu> wrote in message <h3okt9$bng$1@fred.mathworks.com>...
> Currently, I am trying to design a GUI that has a running clock. I assumed that the best way to do it is the make a master function, "Garbage," start an infinite while loop, and embed all of the callbacks for my GUI in the infinite loop. For some reason, MATLAB doesn't like while statements that stretch across the entire program, because it says that it is an invald use of the function keyword. Any and all help would be greatly appreciated.
>
>
> *********************************************************************
> handles.ticFlag = 1
> handles.startFlag = 1
> function garbage(hObject, eventdata, handles)
>
> while handles.ticFlag == 1
>
> handles.difference = toc - handles.startTime
> if handles.startFlag == 1;
> handles.timerDifference = floor(handles.difference);
> if handles.timerDifference < 10
> handles.displayedTime = horzcat('00:00:0', num2str(handles.timerDifference));
> elseif handles.TimerDifference < 60
> handles.displayedTime = horzcat('00:00:', num2str(handles.timerDifference));
> end
> set(handles.Timer, 'string', handles.displayedTime);
> end
>
> % --- Executes on key press with focus on figure1 and no controls selected.
> function figure1_KeyPressFcn(hObject, eventdata, handles)
> % hObject handle to figure1 (see GCBO)
> % eventdata reserved - to be defined in a future version of MATLAB
> % handles structure with handles and user data (see GUIDATA)
> end %end figure1_KeyPress
>
> end %end while statement
>
> end %end function Garbage
> *********************************************************************
Hi,
why do you need "figure1_KeyPressFcn" inside while loop.
try to have this function declaretion & statements after while loop but call this function inside while loop.
& any function should start with the function name followed by arguments & statements .So remove the top two lines & put it after function garbage()
& Retry....
all the best....
Shan.....

Subject: Nested Function Error?

From: Bill

Date: 17 Jul, 2009 17:58:01

Message: 4 of 5

Shan, the reason why I wanted KeyPressFcn in the while loop was because I'm trying to have a running timer in the GUI I am using. The problem is that while this clock is running, I need to be able to do other things, press other buttons, etc., and also at the same time be able to pause/stop the clock if need be. I not understand that I cannot nest GUI functions in a While Loop, but I am still without a solution. Currently, my solution, enclosed below works in terms of updating the clock, but fails to properly execute the stop and pause commands.

*********************************************************************
handles.ticFlag = 0;
handles.whileInitializer = 1;
handles.escapeFlag = 0;

function figure1_KeyPressFcn(hObject, eventdata, handles)
Key = get(gcf, 'CurrentKey');
handles.Key = Key;

switch(handles.Key)
    case 'f12' %%start key
        if handles.ticFlag == 0
            handles.startFlag = 1;
            handles.ticFlag = 1;
            beep;

            tic;
            while (handles.whileInitializer == 1) && (handles.escapeFlag ~= 1)
                handles.endTime = toc;
                handles.elapsedTime = handles.endTime;

                if handles.elapsedTime >= .1
                     %%In this section, I have code to parse out the seconds into minutes, etc. which I end up calling handles.displayedTime I didn't feel it necessary to include this.
                end
                set(handles.Timer, 'string', handles.displayedTime);
            

                drawnow;
                handles.Key=[];
            end

    case 'escape' %%Stop key
        if handles.escapeFlag == 0
            handles.whileInitializer = 0;
            handles.escapeFlag = 1;
            set(handles.Timer, 'string', 'Stopped');
            set(handles.Timer, 'ForegroundColor', [1 0 0]);
            guidata(hObject, handles);

            %%This will reset all settings back to the starting point.

        elseif handles.escapeFlag == 1
            handles.escapeFlag = 0;
            handles.pauseFlag = 0;
            set(handles.Timer, 'string', '00:00:00');
            set(handles.Timer, 'ForegroundColor', [1 1 0]);

            %%This will restart the program and restart all settings back to t=0
        end

etc...
*********************************************************************

I hope I explained it clearly. Basically, I want to ability to start and stop a timer that basically runs in the background of the GUI while I am able to do other things. I want the timer to be accessible, such as for whether or not I should modify things in the GUI, i.e. change the location of an axes over time, changes colors at certain times, etc.

Subject: Nested Function Error?

From: Bruno Luong

Date: 17 Jul, 2009 18:43:03

Message: 5 of 5

help TIMER

Use the real TIMER objects instead of hacking loosely Matlab syntax rules. It sure did not work.

Bruno

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
nested functions Sprinceana 17 Jul, 2009 03:33:04
rssFeed for this Thread
 

MATLAB Central Terms of Use

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 Terms prior to use.

Contact us at files@mathworks.com