I keep getting an error about my inputs in a function I am trying to create?

1 visualización (últimos 30 días)
function output = dim_check(dimension,name)
% DIM_CHECK confirms if the user entered a valid number for a dimension
% Input: A scalar value for dimension, and a string that stands for what
% the value (dimension) represents
% Output: The dimension
% Processing: The function will accept the dimension and set it equal to
% the output. It will test to see if it is <= 0. If it is <= 0, then the
% function will use the while function as a loop to ask the user to input
% another dimension. A message will be displayed using the string inputed.
% The statement will tell the user that dimension type (found from the
% string) they inputed cannot be equal to 0.
output = dimension;
'name' = name
while (output <=0)
fprintf('The %s cannot be less than or equal to 0.\n',name)
output = input('Enter a value for your dimension. ');
end
end

Respuesta aceptada

David Sanchez
David Sanchez el 23 de Oct. de 2013
function output = dim_check(dimension,name)
output = dimension;
'name' = name % ---------> this is the cause of your error. It has no sense. If you comment out this line (or delete it), your function will work
while (output <=0)
fprintf('The %s cannot be less than or equal to 0.\n',name)
output = input('Enter a value for your dimension. ');
end
end
  2 comentarios
Michael
Michael el 23 de Oct. de 2013
function [output] = dim_check(dimension,name)
% DIM_CHECK confirms if the user entered a valid number for a dimension % Input: A scalar value for dimension, and a string that stands for what % the value (dimension) represents % Output: The dimension % Processing: The function will accept the dimension and set it equal to % the output. It will test to see if it is <= 0. If it is <= 0, then the % function will use the while function as a loop to ask the user to input % another dimension. A message will be displayed using the string inputed. % The statement will tell the user that dimension type (found from the % string) they inputed cannot be equal to 0.
output = dimension;
while (output <=0) fprintf('The %s cannot be less than or equal to 0.\n',name) display('Enter a value that is greater than 0. ') output = input('Enter a value for your dimension. '); end end
This is what I have now and it sill gives me the same error mesage
David Sanchez
David Sanchez el 23 de Oct. de 2013
could you paste the error, please?
How are you calling the function? You have to call it like this:
my_output = dim_check(33,'my_dim')

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by