简体   繁体   中英

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. 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.

You don't have to use globals. You can transfer the data using the handles variable, which is the standard methodology of 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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