Hello. Is there a way to set an inputdlg box to only allow 5 characters? For instance is someone tries to type 6 character it just doesnt let them?

조회 수: 6 (최근 30일)
.

답변 (2개)

Neha
Neha 2023년 5월 2일
I understand that you want to create a dialog box containing a field which can accept a user input of fixed length. Please refer to the code given below, the function uses a while loop to keep displaying the inputdlg box until the user enters a valid input.
The validation function checks if the input is exactly 5 characters. If the input is valid, the function exits the loop and returns the input. If the input is invalid, the function displays an error message and sets the default input to the previous value, so the user can try again.
function input = myInputDlg()
prompt = {'Enter a 5-character string:'};
dlgtitle = 'Input';
dims = [1 35];
definput = {''};
input = '';
while true
answer = inputdlg(prompt, dlgtitle, dims, definput);
if isempty(answer)
% User clicked cancel
return;
end
input = answer{1};
if numel(input) == 5
break;
else
errordlg('Input must be exactly 5 characters.','Input Error','modal');
definput = {input};
end
end
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 5월 2일
This does not, however, satisfy the requirement that the inputdlg "just doesn't let them" input the 6th character.
Scotty Mac
Scotty Mac 2023년 12월 6일
I modified slightly to check if an input filename is greater than 16 characters...
prompt = {'PLEASE ENTER AN OUTPUT FILENAME.'};
dlgtitle = 'Output File';
dims = [1 35];
definput = {''};
outputfile = '';
while true
answer = inputdlg(prompt, dlgtitle, dims, definput);
if isempty(answer)
% User clicked cancel
return;
end
outputfile = answer{1};
if numel(outputfile) <= 16
% Check if filename should be default
if (outputfile == "")
% Use default if empty
outputfile = 'default_filename';
end
else
errordlg('Filename size is 16 characters maximum!','Input Error','modal');
definput = {outputfile};
end
end

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2023년 5월 2일
No, inputdlg() has no provision for this.
I do not know what the options are for uifigure (app designer).
For traditional figure(), what you would need to do is use a KeyPressFcn callback on a uicontrol style 'edit'. The KeyPressFcn would be responsible for keeping track of every character press -- because inside KeyPressFcn for uicontrol, the String property is not updated until the user presses return or the control loses focus. The KeyPressFcn would update the String property for 1 to 5 characters, and would just stop updating the String property for additional characters.
In such a situation, it is recommended that the KeyPressFcn also detect backspace or delete, to permit the user to erase a character that has already been entered (in which case the callback should decrement the internal character count and remove the character from the String property.) However, going as far as supporting arrow keys and cursor positioning to support editing in the middle of the string is probably more trouble than it is worth.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by