我的目标是从图像中创建一个切换按钮,并能够通过其他功能检索切换按钮的状态。我通过在userdata中存储信息并相应地处理该信息来完成此操作。但是,在某些函数中检索userdata时遇到了问题。我遇到问题的代码如下所示:
% --- Executes on mouse press over axes background.
function image1_Select(hObject, eventdata, handles)
data = get(handles.axes1, 'UserData');
if data == '1' % was enabled
data = '0';
set(handles.button, 'enable', 'off') %controls a different button
image = imread('im1.jpg');
set(handles.axes1, 'CData', image) %when pressed, the image displayed toggles
elseif data == '0' % was disabled
data = '1';
set(handles.button, 'enable', 'on')
image = imread('im2.jpg');
set(handles.axes1, 'CData', image)
else
errordlg('There has been an error, try again', 'Error')
data = '0';
set(handles.button, 'enable', 'off')
image = imread('im1.jpg');
set(handles.axes1, 'CData', image)
end
set(handles.axes1, 'UserData', data)
% --- Executes on mouse press over axes background.
function image2_Select(hObject, eventdata, handles)
data = get(handles.axes2, 'UserData');
if data == '1' % was enabled
data = '0';
set(handles.button2, 'enable', 'off')
image = imread('otherim1.jpg');
set(handles.axes2, 'CData', image)
elseif data == '0' % was disabled
data = '1';
set(handles.button2, 'enable', 'on')
image = imread('otherim2.jpg');
set(handles.axes2, 'CData', image)
else
errordlg('There has been an error, try again', 'Error')
data = '0';
set(handles.button2, 'enable', 'off')
image = imread('otherim1.jpg');
set(handles.axes2, 'CData', image)
end
set(handles.axes2, 'UserData', data)
testdata = get(handles.axes1, 'UserData')
function load(hObject, eventdata, handles)
data1 = get(handles.axes1, 'UserData')
data2 = get(handles.axes2, 'UserData')
%other code, the above are the first lines in the function
testdata
返回' 1' (如果之前调用set1
)但data1
和data2
返回[ ]
时应返回“{1}}”因为在set1
之前调用了set2
和load
发生了什么事?
谢谢!