How to implement the arguments validation in a function correctly?
    18 ビュー (過去 30 日間)
  
       古いコメントを表示
    
    Ana Gabriela Guedes
 2022 年 10 月 15 日
  
    
    
    
    
    回答済み: Vinayak Choyyan
    
 2022 年 10 月 18 日
            Hello!! 
I am writing a code for neural networks where the user can choose if he wants to use a filter, how many layers, the activation functions to use, etc. I wanted to set an argument validation so the only options the user can choose are already defined.  I tried to write it this way but I always get an error. Any sugestion on how to fix it? Thank you!
The error i get is the following:
 >> Error using cell/ismember
Input A of class double and input B of class cell must be cell arrays of character vectors, unless one is a character vector.
Error in mustBeMember (line 14)
if ~all(ismember(A, B), 'all')
    Error in main (line 12)
    nLayers (2,:) double {mustBeMember(nLayers,{1,2})}
>>
function main(filter,nLayers,activFunction1,activFunction2,softmax,testOrTrain)
arguments
    filter (1,:) string {mustBeMember(filter,{'no','AM','BP'})}
    nLayers (2,:) double {mustBeMember(nLayers,{1,2})} 
    activFunction1 (3,:) string {mustBeMember(activFunction1,{'-','hardlim','purelin','logsig'})}
    activFunction2 (4,:) string {mustBeMember(activFunction2,{'hardlim','purelin','logsig'})}
    softmax (5,:) string {mustBeMember(softmax,{1,2})}
    testOrTrain (6,:) string {mustBeMember(testOrTrain,{'train','test'})}
end
... % function itself
end
0 件のコメント
採用された回答
  Vinayak Choyyan
    
 2022 年 10 月 18 日
        Hi,
As per my understanding, you are trying to use Function Argument Validation and you are getting the error you are mentioned above.
Please try the following code snippet:
function main(filter,nLayers,activFunction1,activFunction2,softmax,testOrTrain)
arguments
    filter (1,:) string {mustBeMember(filter,{'no','AM','BP'})}
    nLayers (2,:) double {mustBeMember(nLayers,[1,2])}   %change here
    activFunction1 (3,:) string {mustBeMember(activFunction1,{'-','hardlim','purelin','logsig'})}
    activFunction2 (4,:) string {mustBeMember(activFunction2,{'hardlim','purelin','logsig'})}
    softmax (5,:) double {mustBeMember(softmax,[1,2])}   %change here
    testOrTrain (6,:) string {mustBeMember(testOrTrain,{'train','test'})}
end
... % function itself
end
You were using {1,2} which creates a cell array. I believe you intended to use [1,2] which creates and array. I also changed the data type from string to double for softmax. You can read more about Function Argument Validation
and check out some examples of using mustBeMember 
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
				Help Center および File Exchange で Deep Learning Toolbox についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

