Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Trouble validating number of parameters in a function

1 visualizzazione (ultimi 30 giorni)
PR
PR il 2 Feb 2015
Chiuso: MATLAB Answer Bot il 20 Ago 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

Risposte (4)

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

PR
PR il 2 Feb 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 il 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

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

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by