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

时间:2012-06-10 11:54:17

标签: matlab global-variables matlab-figure

我想在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,但它无效。

1 个答案:

答案 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);