フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Trouble validating number of parameters in a function

1 回表示 (過去 30 日間)
PR
PR 2015 年 2 月 2 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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

回答 (4 件)

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

PR
PR 2015 年 2 月 2 日
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 2015 年 2 月 2 日
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 2015 年 2 月 3 日
Thanks Guillaume. I appreciate you taking the time to develop that function. I will try and integrate some of your strategies!

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by