Info
This question is closed. Reopen it to edit or answer.
Trouble validating number of parameters in a function
1 view (last 30 days)
Show older comments
To whom it may concern:
I am a beginner in Matlab programming and I am having difficulty with the function below. I need to validate that one of the inputs b1,b2,h,h1,l,v is [ ]. In otherwords, there can only be one unknown. The function is supposed to return 1 if there is only ony unknown, 0 if there are too many or too little. Not sure if I am on the right track but I am limited to basic Matlab vocabulary!
Thanks in advance.
MY FUNCTION:
function [ nb_valid_parameters ] = validate_nb_parameters( b1, b2, h, h1, l, v )
if (isempty(b1) & isnumeric(b2) & isnumeric(h) & isnumeric(h1) & isnumeric(l) & isnumeric(v));
nb_valid_parameters = 1;
if (isnumeric(b1) & isempty(b2) & isnumeric(h) & isnumeric(h1) & isnumeric(l) & isnumeric(v));
nb_valid_parameters = 1;
% end
if (isnumeric(b1) & isnumeric(b2) & isempty(h) & isnumeric(h1) & isnumeric(l) & isnumeric(v));
nb_valid_parameters = 1;
% end
if (isnumeric(b1) & isnumeric(b2) & isnumeric(h) & isempty(h1) & isnumeric(l) & isnumeric(v));
nb_valid_parameters = 1;
% end
if (isnumeric(b1) & isnumeric(b2) & isnumeric(h) & isnumeric(h1) & isempty(l) & isnumeric (v));
nb_valid_parameters = 1;
% end
if (isnumeric(b1) & isnumeric(b2) & isnumeric(h) & isnumeric(h1) & isnumeric(l) & isempty(b1));
% end
else nb_valid_parameters = 0;
end
end
end
end
end
fprintf('\n\n the number of parameters is invalid \n\n');
end
end
0 Comments
Answers (4)
Torsten
on 2 Feb 2015
Hint:
Use
summe=isempty(b1)+isempty(b2)+isempty(h)+isempty(h1)+isempty(l)+isempty(v)
Best wishes
Torsten.
0 Comments
Guillaume
on 2 Feb 2015
Because I like to write code that is as generic as possible, I'd use varargin and expansion of cell arrays to comma separated list to perform the test and allocate the inputs. So, if in the future there's some additional variables, there's only one line to change:
function [nb_parametres_valides] = valider_nombre_parametres(varargin)
inconnues_voulues = 1;
variables_voulues = 6;
nb_inconnues = sum(cellfun(@isempty, varargin);
nb_variables = numel(varargin);
if nb_variables ~= variable_voulues
fprintf('il n'y pas assez de variables en entree');
end
switch sign(nb_inconnues - inconnues_voulues)
case -1
fprintf('il n y pas assez d''inconnues en entree');
case 0
fprintf('le nombre de parametres en entree est valide');
case 1
fprintf('il y a trop d''inconnues en entree');
end
%and if you want to access the input variables:
%[b1, b2, h, h1, l, v] = varargin{:};
end
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!