フィルターのクリア

inputdlg cancel press

45 ビュー (過去 30 日間)
Shivganga
Shivganga 2012 年 4 月 7 日
コメント済み: Image Analyst 2019 年 4 月 10 日
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,

回答 (3 件)

Walter Roberson
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.

Image Analyst
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 件のコメント
Shivganga
Shivganga 2012 年 4 月 7 日
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 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
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
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 ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by