필터 지우기
필터 지우기

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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by