Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Strange KeyPressFcn behavior

Subject: Strange KeyPressFcn behavior

From: Andy Eisenberg

Date: 6 Jul, 2009 14:44:02

Message: 1 of 3

I'm building a GUI programmatically and I'm noticing some unexpected behavior that I can't figure out how to fix. I have an edit text box and a button next to it such that when the button is pressed, the string from the edit text box gets added to a list box. The button should also clear the edit text box (set the string to ''), and return control to it. Since I didn't want to have to go to the mouse so often, I thought I'd add a KeyPressFcn to catch 'return' and call the push button callback.

The problem I've been having is that when I use 'return' to call the push button callback, empty strings are added to the list box even though they shouldn't be. (The push button, however, works correctly.) This doesn't make any sense to me, since all of the list box updates happen in the push button callback, and the key press function just calls the push button callback.

Can anybody help me figure out this strange behavior? The relevant callbacks are below.

    function yaddtolistPush_Callback(src,eventdata)
        % Adds a formula typed into yaxisEdit to yaxisList
        
        % Get the entered string and current list
        formula=get(yaxisEdit,'String');
        currlist=get(yaxisList,'String');
        
        % Check that formula is not empty
        if ~strcmp(formula,'')
            % Add formula to list
            len=length(currlist);
            newlist=currlist;
            newlist{len+1,1}=formula;
            set(yaxisList,'String',newlist);

            % Clear from edit box and return control to yaxisEdit
            set(yaxisEdit,'String','');
            uicontrol(yaxisEdit);
        else
            uicontrol(yaxisEdit);
        end
    end

    function yaxisEdit_KeyPressFcn(src,eventdata)
        % The KeyPressFcn catches 'Enter' so that the user can add formulas
        % to the list without using the button
        
        % Get the pressed button
        pressed=eventdata.Key;
        if strcmp(pressed,'return')
            drawnow;
            yaddtolistPush_Callback(yaddtolistPush,[]);
        end
    end

Subject: Strange KeyPressFcn behavior

From: Zhelyazko

Date: 6 Jul, 2009 22:00:04

Message: 2 of 3

Change the ~strcmp(formula,'')
with ~isempty(formula)
because when you push enter your string is going to be formula= Empty string: 1-by-0
strcmp(formula,'') will be 0

Subject: Strange KeyPressFcn behavior

From: Andy Eisenberg

Date: 7 Jul, 2009 12:53:00

Message: 3 of 3

"Zhelyazko " <ztumbev@yahoo.com> wrote in message <h2ts54$nch$1@fred.mathworks.com>...
> Change the ~strcmp(formula,'')
> with ~isempty(formula)
> because when you push enter your string is going to be formula= Empty string: 1-by-0
> strcmp(formula,'') will be 0

Thanks! This solved the problem.

Tags for this Thread

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.

Contact us