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
채택된 답변
OCDER
2018년 8월 2일
I think input parser + varargin is what you need here.
- https://www.mathworks.com/help/matlab/ref/inputparser.html
- https://www.mathworks.com/help/matlab/ref/varargin.html
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
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.
- parse the user inputs - make sure they are valid inputs
- use switch statements to decide what to do for each condition
- 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
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!
추가 답변 (1개)
federica pasquali
2018년 8월 5일
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 .
카테고리
도움말 센터 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
