HOW TO CHECK THE PARAMETERS RECEIVED IN THE FUNCTION CALL?

dear all,
this is my function : function [] = print( value1 , value2, value3 ,value4,value5,value6,value7,value8)
value1 and value 2 are REQUIRED the others are OPTIONAL . I need to know how to check which parameters i've received and their value , because some of them could be numbers , characters . and i don't know how to do that. thanks

 Réponse acceptée

I think input parser + varargin is what you need here.
Example:
%Changed the name "print" to "print2" to prevent overriding matlab built-in "print" function
function [] = print2(varargin)
P = inputParser;
addRequired(P, 'value1', @ischar);
addRequired(P, 'value2', @isnumeric);
addOptional(P, 'value3', [], @(x) ischar(x) || isnumeric(x));
parse(P, varargin{:});
P = P.Results; %Just so you can refer to value1 as P.value1, not P.Results.value1 (downside to inputParser)
%To get value1, use P.value1
Alternatively, you'll have to make your own input-parsing function

10 commentaires

i really appreciate your help . and i need you again , if it's not a problem for you , i'm trying to do but ... so this is my function : function [] print2 = ( sequenceId , folder , varargin)
P = inputParser ; % sequenceId is = '1b1c' in the calling function % folder = 'folder adress' in the calling function % and now the optional parameters : % Ch ='A' is a letter in the calling function % x % y % = numbers in the calling function % d = 'Ball' or 'Space'.. it's a choice in a list of words & c = 'Amin' or 'Tra'.. it's a choice in a list of words
after i want to see which parameters i have to use this parameters in others instruction but i need to know if Ch is = 'A' or Ch = 'B' the same for D and C and i don't know hoe to do it hope you can help me
I believe you need a switch statement for your input variable, so that you can switch between parameters. Example:
switch upper(Ch)
case 'A'
Param = [1 2 3 4]
case 'B'
Param = [2 4 6 8]
end
is not there a way to see at the end of the inputparser which values I have and which I do not have and read the value?
The mainthing you can do with inputParser is to make sure the user inputs the correct things. You can check if user input a correct value for "Ch" for example via something like this:
addOptional(P, 'Ch', 'A', @(x) ischar(x) && any(strcmpi(x, {'A', 'B', 'C', 'D'})))
If you want to take different courses of action depending on the input, then you need a separate step AFTER input parsing, as I've show via the switch.
  1. parse the user inputs - make sure they are valid inputs
  2. use switch statements to decide what to do for each condition
  3. run your main computation based on the input and parameters set for the input
Or did I misunderstood?
OR, maybe it's better if you use name-value input parsing, where the user should specifically tell what values they are giving the function.
addParameter(P, 'Ch', 'A', ...)
With this option, user must give name-value ('Ch', 'A') pairs to the function:
myFunction(V1, V2, 'Ch', 'A')
That way, you know that the user DID give a value for 'Ch'. Otherwise, having to determine what users did/did not provide will push a lot of input parsing work to you. You'll have to go the custom input parsing route and do a lot of checks via "if" statements.
he problem is this: - check which inputs have been entered in the function call as there are 6 optional parameters - see what value the optional parameters have in order to do different operations depending on what the user wants to do
in the function call if I do not want 'Ch' but I put x y z I do not want the position of x to become the value of Ch function [] = print2 (required1, required2, ch, x, y, z) but I do not know how to do what I need to do
OCDER
OCDER le 3 Août 2018
Modifié(e) : OCDER le 3 Août 2018
With name-value pairing, you don't have to worry about the ordering of inputs.
Assuming you are using addParameter instead of addOptional, your function would now be used as this:
print2(req1, req2, 'Ch', 'B', 'x', 1, 'y', 2)
It's the same as this:
print2(req1, req2, 'x', 1, 'Ch', 'B', 'y', 2)
And you can omit some parameters to use default values.
print2(req1, req2, 'Ch', 'B', 'y', 2) %Uses a default x value
As you can see, user doesn't even have to specify 'x', 'z', etc, if these are optional inputs - inputParser will use the default values you give. addParameter can be viewed as addOptional with capabilities to be inputted in any order.
function [] = prova(ID , Folder , varargin)
P = inputParser;
addRequired( P , 'ID' );
addRequired( P , 'Folder' );
addOptional( P , 'Chain' , '' , @(x) ischar(x) );
addOptional( P , 'X' , [] , @(x) isnumeric(x) );
addOptional( P , 'Y' ,[] , @(x) isnumeric(x) );
addOptional( P , 'Z' ,[] , @(x) isnumeric(x) );
addOptional( P , 'Display' ,'', @(x) ischar(x) );
addOptional( P , 'Color' ,'' , @(x) ischar(x) );
parse(P , ID , Folder , varargin {:});
P = P.Results
end
if i'm calling prova('1a',folder,'b') it's going to work. but if i'm calling prova('1a',folder,30,40,60) without chain (before ='b') doesn't work , i received this error :The value of 'Chain' is invalid. It must satisfy the function: @(x)ischar(x). so i've tried to use try catch like this
try
addOptional( P , 'Chain' , '' , @(x) ischar(x) );
catch ME
end
but it doesn't work and i don't know how ho to built a ME object .
If you want variable types of inputs like
prova('1a',folder,'b')
prova('1a',folder,30,40,60)
then you'll have to make more input parsing checks + custom checking.
function [] = prova(ID , Folder , varargin)
if nargin > 3 & strcmpi(varargin{1}, {'b', 'r', ...'})
Chain = varargin{1};
varargin = varargin(2:end);
else
Chain = '';
end
P = inputParser;
addRequired( P , 'ID' );
addRequired( P , 'Folder' );
%%addOptional( P , 'Chain' , '' , @(x) ischar(x) ); %%DELETE. You did the parsing earlier, separately, as it's special
addOptional( P , 'X' , [] , @(x) isnumeric(x) );
addOptional( P , 'Y' ,[] , @(x) isnumeric(x) );
addOptional( P , 'Z' ,[] , @(x) isnumeric(x) );
addOptional( P , 'Display' ,'', @(x) ischar(x) );
addOptional( P , 'Color' ,'' , @(x) ischar(x) );
parse(P , ID , Folder , varargin {:});
P = P.Results
The other option, if you want a "cleaner" version, is to make sure Display, Color, and Chain are "addParameter" options
%prova('1a',folder, 'Chain', 'b')
%prova('1a',folder,X, Y, Z, 'Display', 'none', 'Chain', 'b', 'Color', 'r')
function Out = prova(ID, Folder, varargin)
P = inputParser;
addRequired( P , 'ID' );
addRequired( P , 'Folder' );
addOptional( P , 'X' , [] , @(x) isnumeric(x) );
addOptional( P , 'Y' ,[] , @(x) isnumeric(x) );
addOptional( P , 'Z' ,[] , @(x) isnumeric(x) );
addParameter( P , 'Chain' , '' , @(x) ischar(x) ); %<-- addParameter
addParameter( P , 'Display' ,'', @(x) ischar(x) ); %<-- addParameter
addParameter( P , 'Color' ,'' , @(x) ischar(x) ); %<-- addParameter
parse(P , ID , Folder , varargin {:});
P = P.Results
I really want to thank you for the help. you were very kind
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (1)

function [] = prova(ID , Folder , varargin)
P = inputParser;
addRequired( P , 'ID' );
addRequired( P , 'Folder' );
addOptional( P , 'Chain' , '' , @(x) ischar(x) );
addOptional( P , 'X' , [] , @(x) isnumeric(x) );
addOptional( P , 'Y' ,[] , @(x) isnumeric(x) );
addOptional( P , 'Z' ,[] , @(x) isnumeric(x) );
addOptional( P , 'Display' ,'', @(x) ischar(x) );
addOptional( P , 'Color' ,'' , @(x) ischar(x) );
parse(P , ID , Folder , varargin {:});
P = P.Results
end
if i'm calling prova('1a',folder,'b') it's going to work. but if i'm calling prova('1a',folder,30,40,60) without chain (before ='b') doesn't work , i received this error :The value of 'Chain' is invalid. It must satisfy the function: @(x)ischar(x). so i've tried to use try catch like this
try
addOptional( P , 'Chain' , '' , @(x) ischar(x) );
catch ME
end
but it doesn't work and i don't know how ho to built a ME object .

Catégories

En savoir plus sur Argument Definitions dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by