inputdlg cancel press
40 次查看(过去 30 天)
显示 更早的评论
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,
0 个评论
回答(3 个)
Walter Roberson
2012-4-7
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.
0 个评论
Image Analyst
2012-4-7
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 个评论
Image Analyst
2012-4-7
编辑:Image Analyst
2019-4-10
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
Abdelmalek Benaimeur
2019-4-10
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 个评论
Image Analyst
2019-4-10
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!