Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Trouble validating number of parameters in a function
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (4개)
Torsten
2015년 2월 2일
Hint:
Use
summe=isempty(b1)+isempty(b2)+isempty(h)+isempty(h1)+isempty(l)+isempty(v)
Best wishes
Torsten.
댓글 수: 0
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
댓글 수: 0
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!