简体   繁体   English

Matlab中另一个函数的访问变量

[英]Access Variable of one function in another function in Matlab

I want to access the value of a variable in one function in another function in matlab GUI. 我想在Matlab GUI的另一个函数中访问一个函数中变量的值。 eg 例如

    % --- 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);

Here I want to access CovImg in function browseSecImg_Callback from function browseCoverHide_Callback but it is not working. 在这里,我想访问CovImgfunction browseSecImg_Callbackfunction browseCoverHide_Callback ,但它无法正常工作。

You don't have to use globals. 您不必使用全局变量。 You can transfer the data using the handles variable, which is the standard methodology of GUIDE . 您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM