Can I loop through arguments and loop back to original names

I have a function that can take in a value to check and a min value and max value to validate if the value to check is in range. The function though also error handles if any of the arguments come in from a input('prompt', 's') function.
So I have the following which works:
function [isValid, errorMessage] = validateNumberWithRange(value, min, max)
...
inputs = {value, min, max};
for i = 1:numel(inputs)
if ~isnumeric(inputs{i})
inputs{i} = str2double(inputs{i});
if isnan(inputs{i})
errorMessage = "All values must be numeric, please check your inputs.";
isValid = false;
end
end
end
end
The problem is let's say value = '500'
The loop will make inputs{1} = 500 without the single quotes and be numeric but at the end of the loop value is still '500' so I have to do one of the following:
Version 1:
value = inputs{1};
min = inputs{2};
max = inputs{3};
if value < min || value > max
errorMessage = "The Value is not within range, please check your inputs.";
isValid = false;
end
Version 2:
if inputs{1} < inputs{2} || inputs{1} > inputs{3}
errorMessage = "The Value is not within range, please check your inputs.";
isValid = false;
end
Both ways work, but already just don't feel right to me and could get even uglier if there were more arguments.
Is there a way to in the loop or with another loop, get any type conversions that had to be done through inputs{} back to their original variable names?

2 comentarios

Is it necessary to use input? Why not directly provide the values to the function?
If it is necessary to use input(), you can convert the values to numeric values via str2double.
If you have the Symbolic Math Toolbox, you can also use sym and then compare. (Symbolic values are represented in their exact form, while double values have a limited precision)
It's a reusable function that at times another function will be feeding it values from a file but in some on demand situations there's another function that prompts a user for the inputs and that's where this function does convert it from str2double. As mentioned, what I've got is working, I'm just wondering if there's a cleaner or simpler way to do it because in this example it's not too bad with only 3 arguments I'm worrying about, but if I made a similar function in the future with more arguments, I'm worried it could start to look clunky with version 1 above or unreadable with version 2 above were I've lost meaningful variable names.

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 30 de Jun. de 2023
Editada: Matt J el 30 de Jun. de 2023
Is this what you mean?
inputs = {value, min, max};
for i = 1:numel(inputs)
...
end
[value,min,max]=inputs{:};

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Preguntada:

el 30 de Jun. de 2023

Comentada:

el 30 de Jun. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by