MATLAB GUI回调未为另一个回调设置值?

MATLAB GUI回调未为另一个回调设置值?,matlab,matlab-guide,handles,Matlab,Matlab Guide,Handles,我希望一次按下按钮来设置值,另一次按下按钮来输出变量的值。似乎该值不是通过使用以下代码第一次按下按钮来设置的。为什么呢 % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined

我希望一次按下按钮来设置值,另一次按下按钮来输出变量的值。似乎该值不是通过使用以下代码第一次按下按钮来设置的。为什么呢

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.dog=1001

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles
disp(num2str(handles.dog))   % <-- value not present
%---在按下按钮2时执行。
函数pushbutton2\u回调(hObject、eventdata、句柄)
%HOBOT手柄至按钮2(见GCBO)
%eventdata保留-将在未来版本的MATLAB中定义
%带有句柄和用户数据的句柄结构(请参见GUI数据)
把手。狗=1001
%---在按下按钮3时执行。
函数pushbutton3\u回调(hObject、eventdata、handles)
%HOBOT手柄至按钮3(见GCBO)
%eventdata保留-将在未来版本的MATLAB中定义
%带有句柄和用户数据的句柄结构(请参见GUI数据)
处理
disp(num2str(handles.dog))%use
guidata(hObject,handles)设置值后保留它

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.dog=1001

guidata(hObject, handles);

您必须编写
guidata(hObject,handles)
在“pushbutton2\u”回调的末尾更新句柄结构,以便可以从其他函数访问它

因此,生成的代码将是:

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.dog=1001
guidata(hObject, handles);

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles
disp(num2str(handles.dog))   % <-- value not present
%---在按下按钮2时执行。
函数pushbutton2\u回调(hObject、eventdata、句柄)
%HOBOT手柄至按钮2(见GCBO)
%eventdata保留-将在未来版本的MATLAB中定义
%带有句柄和用户数据的句柄结构(请参见GUI数据)
把手。狗=1001
guidata(hObject、handles);
%---在按下按钮3时执行。
函数pushbutton3\u回调(hObject、eventdata、handles)
%HOBOT手柄至按钮3(见GCBO)
%eventdata保留-将在未来版本的MATLAB中定义
%带有句柄和用户数据的句柄结构(请参见GUI数据)
处理
disp(num2str(handles.dog))%