[英]Access Variable of one function in another function in Matlab
我想在Matlab GUI的另一个函数中访问一个函数中变量的值。 例如
% --- 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
,但它无法正常工作。
您不必使用全局变量。 您可以使用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.