MATLAB: How to cancel the save program

guiguideMATLABsave

Hye guyz. I need some help here. I have this coding:
fileName = inputdlg('Please enter the name for your figures');
directoryName = uigetdir('','Please select a folder to save to');
if directoryName == 0 %# User pressed the "Cancel" button...
directoryName = ''; %# ...so choose the empty string for the folder
end
filePath = fullfile(directoryName,fileName{1}); %# Create the file path
extensions = {'fig','bmp'};
for k = 1:length(extensions)
saveas(gcf,filePath,extensions{k}); %# Save the file
set(gcf,'PaperPositionMode','auto');
end
This coding have some problem. When i run this coding, it occurs this error:
??? Index exceeds matrix dimensions.
Error in ==> fyp_editor>uipushtool9_ClickedCallback at 1607 filePath = fullfile(directoryName,fileName{1}); %# Create the file path.
And another thing is when i pressed the cancel button, it kkeep going to filepath. How i want to do something like; when i push the cancel, then it will cancel the save program.

Best Answer

Another way, might be better because the return function can have undesirable consequences.
fileName = inputdlg('Please enter the name for your figures');
directoryName = uigetdir('','Please select a folder to save to');
if directoryName == 0 %# User pressed the "Cancel" button...
directoryName = ''; %# ...so choose the empty string for the folder
else
filePath = fullfile(directoryName,fileName{1}); %# Create the file path
extensions = {'fig','bmp'};
for k = 1:length(extensions)
saveas(gcf,filePath,extensions{k}); %# Save the file
set(gcf,'PaperPositionMode','auto');
end
end