How to distinguish pressing cancel vs entering an empty string in inputdlg?

7 次查看(过去 30 天)
I have an input dialog box that will reopen if an invalid name is entered (it will close if more than 5 attempts is made). It is asking for a name to create a file with.
I want the user to be able to press "cancel" or the "x" (windows close button) and have the program exit, but I also want the user to not type anything and press "ok" and have it count as an incorrect attempt and reopen the box.
However, the only way I found people saying to use this is to check if the input is empty, if it is to break. However, this does not distinguish between meaningfully enterring an empty string and pressing cancel/x.
Is there a way to do what I am trying to do?
Also, if there is a better way to write the code in general, please let me know. I am open to improvements and criticism. This is my first MATLAB script I've written. It's been a journey trying to learn the language/script (coming only from standard C).
Here is the snippet from my code:
while badFileName == true
attemptsLeft = num2str(5 - errorCounter);
outputName = inputdlg(['Please enter a file name. Do not use spaces! Attempts left: ' attemptsLeft],'Output File Creation');
if isempty(outputName)
return;
end
outputName = string(outputName);
badFileName = contains(outputName,illegalCharacters);
errorCounter = errorCounter+1;
if errorCounter >= 5
break
end
end

采纳的回答

Matt J
Matt J 2024-6-14
编辑:Matt J 2024-6-14
However, this does not distinguish between meaningfully enterring an empty string and pressing cancel/x.
It should. Hitting ok with nothing entered wouldn't return an empty result.
Regardless, it would be better to use uiputfile, which automatically forbids empty selections.
while badFileName == true
attemptsLeft = num2str(5 - errorCounter);
outputName = uiputfile('', "Please enter a valid file name. Attempts left: " + attemptsLeft);
if isnumeric(outputName)
return;
end
outputName = string(outputName);
badFileName = contains(outputName,illegalCharacters);
errorCounter = errorCounter+1;
if errorCounter >= 5
break
end
end
  2 个评论
Kealan
Kealan 2024-6-14
编辑:Kealan 2024-6-14
Hey, I thought I had tried printing/showing the variable data in the command window. When I typed nothing and pressed okay or hit cancel, they both came out the same I thought. I do not remember it clearly now, but it may have showed like:
filename = "" or something of the like
Either way, I took in what you said and wrote something similar. I like it much better now. I had them select a file path and and then type a name in a MATLAB dialog box, but using that I was able to do both in one box and it looks/operates much nicer. Also, the windows box already checks for a lot of the illegal characters, so the code is much simpler now. (forgive on the variable names) Take a look:
Is there anything else I can improve on? Also, thank you so much!!
% gather the filename and file path from user
[outputFileName,outputFilePath] = uiputfile('*.txt',"Please select a file destination and name.");
% create the full file path by merging the file path and file name
outputFullFilePath = fullfile(outputFilePath,outputFileName);
% open the file with write permissions
textFile = fopen(outputFullFilePath,'w');
% check if there were errors in opening the file
if textFile == -1
disp('The program ran into issues trying to create the DBC file');
end
Matt J
Matt J 2024-6-14
Also, thank you so much!!
You're quite welcome, but please Accept-click the answer to indicate that it produced a solution for you.
Is ther anything else I can improve on?
Looks good to me!

请先登录,再进行评论。

更多回答(1 个)

Catalytic
Catalytic 2024-6-14
You could also do this -
while badFileName == true
attemptsLeft = num2str(5 - errorCounter);
outputName = inputdlg(['Please enter a file name. Do not use spaces! Attempts left: ' attemptsLeft],'Output File Creation');
if isempty(outputName)
return;
end
outputName = string(outputName);
badFileName = contains(outputName,illegalCharacters)||isempty(outputName{1});
errorCounter = errorCounter+1;
if errorCounter >= 5
break
end
end

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by