变量在 Matlab 中调试与非调试时的行为不同?

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

我使用来自 Matlab R2014a 的 GUI。

我想看看edit控件里有没有写什么。如果是,那么程序会做一些事情。如果不是,则它会执行其他操作。我写的代码是这样的(在 KeyPressFcn 回调中):

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

到目前为止一切顺利。但是,text 变量的值在我调试时与我在 运行 程序本身时似乎有所不同。在调试模式下,一切正常,text 变量获取我从键盘输入的值。当我不调试时,text 变量采用此回调的前一次迭代所具有的值。

例子我想输入数字55,当我输入前5时,text变量为空。当我输入第二个 5 时,text 变量的值为 5.

我哪里搞错了?

我现在无法测试 - 但我怀疑这是因为您正在使用的回调。

KeyPressFcn 当您按下 键时激活 - 这意味着在您按下的键在控件中注册之前 运行 .

您在调试中看到的值您期望 是一种视错觉 - 实际上您没有将实际情况与调试进行比较...