How do I get my code to recognise an empty function input?

function P = poly_input(y)
while
if isempty(y) == 1
y = input('Invalid, no value entered. Enter a value: ', 's');
end
end
end
Basically, i'd like for a re-entry of number message to appear if no value is entered for y!
Please and thanks!

댓글 수: 4

Trouble is... if you've not spotted... y is the value entered in the function (at the top of the question). So if y in the poly_input(y) is blank, then the message appears!
Get rid of your while loop and say
if isempty(y)
% Then use my code with the while loop.
end
% Continue with the rest of the function now since y is valid.
Jessica Parry
Jessica Parry 2013년 4월 7일
편집: Jessica Parry 2013년 4월 7일
sadly this still isnt working how i hoped it would when typing in the command window the function name and a blank matlab still produces an error;
>> poly_input()
Error using Untitled (line 2)
Not enough input arguments.
Image Analyst
Image Analyst 2013년 4월 7일
편집: Image Analyst 2013년 4월 7일
See my comment about how to handle nargin (number of input arguments) being zero.

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

답변 (3개)

Wayne King
Wayne King 2013년 4월 7일
isempty() should work
y = input('What is your favorite color? ','s');
if isempty(y)
y = input('Invalid, no value entered. Enter a value: ', 's');
end
Image Analyst
Image Analyst 2013년 4월 7일
Try this:
while true
% Ask user for a number.
defaultValue = 45;
titleBar = 'Enter a value';
userPrompt = 'Enter the number ';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput) % Doesn't work. Not empty if user input is null.
return;
end; % Bail out if they clicked Cancel.
numericalValue = str2double(cell2mat(caUserInput));
% Check for a valid integer.
if isnan(numericalValue)
% They didn't enter a number.
% They clicked Cancel, didn't enter anything
% or entered a character, symbols, or something else not allowed.
message = sprintf('I said it had to be a number.\nTry again.');
uiwait(warndlg(message));
else
break;
end
end

댓글 수: 3

That's different! If you're going to pass in null from the command line, you should check nargin. Look it up in the help for how to handle it.
if nargin == 0
% Use my while code
end
If I'm not mistaken, this will also require a modification to the function definition:
function P = poly_input(varargin)
if nargin < 1
y = input('Invalid, no value entered. Enter a value: ', 's');
else
y = varargin{1};
end
Nope. This works just fine. [Whether it's preferred or not is a different question. Personally I'd probably have poly_input use narginchk to throw an error if you didn't call it with 1 input.]
function P = poly_input(y)
if nargin < 1
y = input('Enter a value for y: ');
end
P = [y 0];
Call it like this and you will be prompted to enter a value.
P = poly_input()

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

Garret Mans
Garret Mans 2019년 5월 23일
I know I'm only a few years late to this thread, but I'm confused why this question got so overcomplicated.
You just need the input and an initial while loop to catch the empty input. Maybe theres something I'm missing but as far as the question asked, this solves the problem just fine:
function gimmiStuff(y)
while isempty(y)
y = input('You gave me empty stuff. Gimmi stuff: ');
end
% Continue with function
.
.
.
end

카테고리

도움말 센터File Exchange에서 Argument Definitions에 대해 자세히 알아보기

질문:

2013년 4월 7일

답변:

2019년 5월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by