我想在matlab GUI中的另一个函数中访问一个函数中的变量值。 e.g。
% --- Executes on button press in browseCoverHide.
function browseCoverHide_Callback(hObject, eventdata, handles)
% hObject handle to browseCoverHide (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[File,Path] = uigetfile('*.png','Select Image');
path = strcat(Path,File);
global covImg
covImg = imread(path);
axes(handles.axes1);
imshow(covImg);
% --- Executes on button press in browseSecImg.
function browseSecImg_Callback(hObject, eventdata, handles)
% hObject handle to browseSecImg (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global covImg
axes(handles.axes3);
imshow(covImg);
在此,我想从CovImg
访问function browseSecImg_Callback
中的function browseCoverHide_Callback
,但它无效。
答案 0 :(得分:1)
您不必使用全局变量。
您可以使用handles
变量传输数据,这是GUIDE
的标准方法。
% --- Executes on button press in browseCoverHide.
function browseCoverHide_Callback(hObject, eventdata, handles)
% hObject handle to browseCoverHide (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[File,Path] = uigetfile('*.png','Select Image');
path = strcat(Path,File);
handles.covImg = imread(path);
axes(handles.axes1);
imshow(handles.covImg);
guidata(hObject,handles);
% --- Executes on button press in browseSecImg.
function browseSecImg_Callback(hObject, eventdata, handles)
% hObject handle to browseSecImg (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes3);
imshow(handles.covImg);