2012-06-10 134 views
1

我想在matlab GUI中的另一個函數中訪問一個函數中變量的值。 例如訪問Matlab中另一個函數中一個函數的變量

% --- 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,但它無法正常工作。

+1

相似問題:http://stackoverflow.com/q/10772099/97160 – Amro

回答

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); 
+0

謝謝@Andrey。你很棒!!! – Skylark555

相關問題