MATLAB: How to save image using one pushbutton

guideImage Processing Toolboxpushbutton

i add noise in image at axes2, and then i want to save the image.can anyone help me?here is my code that i try
function checkbox1_Callback(hObject, eventdata, handles)
applyNoise(handles);
function applyNoise(handles)
if isfield(handles,'imgData')
imgData = handles.imgData;
noiseStr = '';
if get(handles.checkbox1,'Value')
noiseStr = 'Salt & pepper';
JData = imnoise(imgData,'salt & pepper',0.3);
end
if get(handles.checkbox2,'Value')
if ~isempty(noiseStr)
noiseStr = [noiseStr ', '];
end
noiseStr = [noiseStr 'Gaussian'];
JData = imnoise(imgData,'gaussian',0.1,0.1);
end
if get(handles.checkbox3,'Value')
if ~isempty(noiseStr)
noiseStr = [noiseStr ', '];
end
noiseStr = [noiseStr 'localvar'];
JData = imnoise(imgData,'localvar',0.05*rand(size(imgData)));
end
if get(handles.checkbox4,'Value')
if ~isempty(noiseStr)
noiseStr = [noiseStr ', '];
end
noiseStr = [noiseStr 'poisson'];
JData = imnoise(imgData,'poisson');
end
if get(handles.checkbox5,'Value')
if ~isempty(noiseStr)
noiseStr = [noiseStr ', '];
end
noiseStr = [noiseStr 'speckle'];
JData = imnoise(imgData,'speckle', 0.3);
end
axes(handles.axes2);
%imshow(imgData);
imshow(JData)
title(['Noise type: ' noiseStr,]);
handles.JData = imgData;
%save the handles data
%guidata(hObject,handles);
end
if isfield(handles,'JData')
imgData = handles.JData;
end
and here my save button
function pushbutton2_Callback(hObject, eventdata, handles)
if isfield(handles,'JData')
imgData = handles.JData;
%if isfield(handles,'imgData')
%imgData = handles.imgData;
[Save,savename] = uiputfile('*.jpg','save this file')
fname=fullfile(savename,Save);
imwrite(imgData,fname); % where F is your image
end

Best Answer

Change the declaration to return handles, like this
function handles = applyNoise(handles)
otherwise the changes you make to handles (like adding a field handles.JData) will never be seen by your other functions.