mhaseebn
mhaseebn

Reputation: 551

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

Here I want to access CovImg in function browseSecImg_Callback from function browseCoverHide_Callback but it is not working.

Upvotes: 1

Views: 7946

Answers (1)

Andrey Rubshtein
Andrey Rubshtein

Reputation: 20915

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

Upvotes: 1

Related Questions