Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Trouble validating number of parameters in a function

1 vue (au cours des 30 derniers jours)
PR
PR le 2 Fév 2015
Clôturé : MATLAB Answer Bot le 20 Août 2021
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

Réponses (4)

Torsten
Torsten le 2 Fév 2015
Hint:
Use
summe=isempty(b1)+isempty(b2)+isempty(h)+isempty(h1)+isempty(l)+isempty(v)
Best wishes
Torsten.

PR
PR le 2 Fév 2015
Yes! Thank you Mr. Torsten!
I just had a brainwave (it's in french now though):
function [ nb_parametres_valides ] = valider_nombre_parametres( b1, b2, h, h1, l, v )
nb_inconnues = isempty(b1)+isempty(b2)+isempty(h)+isempty(h1)+isempty(l)+isempty(v);
if (nb_inconnues > 1);
nb_parametres_valides = 0;
fprintf('il y a trop d''inconnues en entree');
end
if (nb_inconnues < 1);
nb_parametres_valides = 0;
fprintf('il n y pas assez d''inconnues en entree');
end
if (nb_inconnues == 1);
nb_parametres_valides = 1;
fprintf('le nombre de parametres en entree est valide');
end
end

Guillaume
Guillaume le 2 Fév 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

PR
PR le 3 Fév 2015
Thanks Guillaume. I appreciate you taking the time to develop that function. I will try and integrate some of your strategies!

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by