Bogdan Doicin
Bogdan Doicin

Reputation: 2416

Variabile behaving differently in debugging vs. non-debugging in Matlab?

I work with the GUI from Matlab R2014a.

I want to see if there is something written in an edit control. If it is, then the program does something. If it is not, then it does something else. The code that I wrote is this (in the KeyPressFcn callback):

h1=findobj('Tag','btnOK1');    %this is a button
h2=findobj('Tag','edIndexIesire'); %this is an edit
text=get(h2,'String'); %I read the content of the edit
msgbox(text); %display it in a message box
if (isempty(text)) %if the edit is empty...
    set(h1,'Enable','off'); %... then disable the button.
else %If it is not...
    set(h1,'Enable','on'); %... enable the button
end

So far so good. However, the value of the text variable seem to be different when I am debugging vs. when I am running the program by itself. In debug mode, everything works fine and the text variable gets the value that I am typing from the keyboard. When I am not debugging, the text variable takes the value what had from the previous iteration of this callback.

Example I want to enter the number 55. When I enter the first 5, the text variable is empty. When I enter the second 5, the text variable has the value 5.

Where am I mistaking?

Upvotes: 0

Views: 38

Answers (1)

matlabgui
matlabgui

Reputation: 5672

I cant test just now - but I suspect this is because of the callback you are using.

The KeyPressFcn is activated when you press the key - that means that it is running before the key you are pressing has registered in the control.

The fact you see the value you expect in debug is a sort of optical illusion - in fact your not comparing the real situation with the debug...

Upvotes: 1

Related Questions