inputdlg cancel press

73 views (last 30 days)
Shivganga
Shivganga on 7 Apr 2012
Commented: Image Analyst on 10 Apr 2019
Hi all, actually in my project it occurs many times that input dialogue box comes but when sometimes I press cancel button in that it gives me error. I want no error even when pressed cancel but just execution stops. so if anybody of you can help me with this. and same thing for imopen command. thanks,

Answers (3)

Walter Roberson
Walter Roberson on 7 Apr 2012
If the user clicks the Cancel button to close an inputdlg box, the dialog returns an empty cell array
So test the result of inputdlg to see if it is the empty cell array, and if it is, do whatever is appropriate in your code to stop the process for that popup box.

Image Analyst
Image Analyst on 7 Apr 2012
Try it like this:
promptMessage = sprintf('Do you want to Continue processing,\nor Cancel to abort processing?');
button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
return; % or break or whatever...
end
imopen(), the Morphological Opening operation in the Image Processing Toolbox, does not use a Cancel button. If you're really talking about uigetfile(), do it this way:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
% Build the complete name of the file the user chose.
fullFileName = fullfile(folder, baseFileName)
  2 Comments
Shivganga
Shivganga on 7 Apr 2012
Actually what I want is to use inputdlg function only but when the box popup n pressed cancel button I don't want error n the process regarding that popup box stops.
Image Analyst
Image Analyst on 7 Apr 2012
Edited: Image Analyst on 10 Apr 2019
OK (you didn't mention that at first), then for inputdlg() do it this way.
% Ask user for a number.
defaultValue = 45;
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, 'Enter the threshold value',1,{num2str(defaultValue)});
if isempty(caUserInput)
% User clicked cancel. Bail out.
return;
end
integerValue = round(str2num(cell2mat(caUserInput)));
% Check for a valid integer.
if isempty(integerValue)
% They didn't enter a number.
% They entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end

Sign in to comment.


Abdelmalek Benaimeur
Abdelmalek Benaimeur on 10 Apr 2019
the best way is this :
try
% tap your code here
catch % this command prevent obtaining error when clicking Cancel button or X button
end
  1 Comment
Image Analyst
Image Analyst on 10 Apr 2019
He wanted to know how to skip the rest of the code so that the error never happens, like by detecting when the user clicked cancel and bailing out, such as
% Ask user for a number.
defaultValue = 45;
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, 'Enter your integer',1,{num2str(defaultValue)});
if isempty(caUserInput)
% User clicked cancel. Bail out. Don't execute any code after this.
return;
end
This is more efficient than continuing on executing code you shouldn't until an error gets thrown (for example, trying to use the value in caUserInput, which is empty, instead of bailing out), and then ignoring the error once it occurs.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!