簡體   English   中英

Matlab中另一個函數的訪問變量

[英]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);

在這里,我想訪問CovImgfunction browseSecImg_Callbackfunction 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM