0

1つのボタンを押して値を設定し、別のボタンを押して変数の値を出力したいと思います。次のコードを使用して最初にボタンを押しても値が設定されていないようです。何故ですか?

% --- 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
4

2 に答える 2

1

`pushbutton2_Callbackの最後に書き込みguidata(hObject, handles);、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);

% --- 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
于 2013-01-16T16:48:37.637 に答える
0

値を設定した後に使用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);
于 2013-01-16T16:48:04.200 に答える