validateattributes() - content of a cell

9 次查看(过去 30 天)
Dear community,
i am struggling with accessing cells in general and the function validateattributes() in specific.
I want to check the input of my function 'myfun(signals,_otherArguments)'. I expect signals to be of type cell and a row vector. This is easy enough:
validateattributes(signals,{'cell'},{'row'},'','signals',1);
Here are two signals for testing purposes:
badSignal = horzcat({'string1','string2',},cell(1,2), [3,4]);
goodSignal = horzcat({'string1','string2',},cell(1,2), 'string3');
Testing those variables will return nothing since they are both of type cell, just as intended.
But furthermore i would like to ensure, that the content of each cell is of type char while using the error massage handling of validateattributes.
This is where i am stuck.
validateattributes(cellfun(@ischar,signals),{'logical'},'positive'},'','signals',1);
This code is able to check each cells type but the error massage is quite useless to the user. For variable badString is Returns:
Expected input number 1, signals, to be positive.
As you can see the Input number -1- is correct but of course the Information 'expected positive' is not what i intended. Another idea is to loop through signals{:} and check every element with validateattributes() but this will mess up the Input number.
I hope I was able to clearify my Problem.
Any ideas how to check the type of each cell of signals while preserving the error handling of validateattributes?

采纳的回答

Adam
Adam 2015-9-18
编辑:Adam 2015-9-18
I think it would be better to move the cellfun outside the validateattributes - i.e. have the validateattributes as the function being applied on each cell rather than putting cellfun inside validateattributes.
e.g.
validationFunc = @(signal), validateattributes( signal, { 'char' }, { },... )
cellfun( validationFunc, signals )
(I haven't checked that syntax on command line, but that is the idea anyway and so far as I can see the syntax is fine, except I missed out your extra arguments for validateattributes that you can obviously add in. I never tend to use those extra arguments personally so just left them off for speed!)
  4 个评论
Marius Mueller
Marius Mueller 2015-9-21
Your Notation did not quite work for me. Instead i had to use:
cellfun( @(signal) validateattributes(...
signal, { 'char' }, { },...), signals);
Note the missing function Name 'validationFunc' and the equals sign.
Since i am using the outdated version MatlabR2011b i do not know whether later Releases might Support your Notation as well. The resulting error message still refers to the internal use of validateattributes:
Error using reduceData
Expected Cellcontent of Signals to be one of these
types:
char
Instead its type was double.
Error in
reduceData>@(signal)validateattributes(signal,{'char'},{},'reduceData','Cellcontent
of Signals') (line 41)
cellfun( @(signal) validateattributes(...
Error in reduceData (line 41)
cellfun( @(signal) validateattributes(...
Nevertheless this should tell the user where the error came from so i will go with this solution.
One final request:
Since you seem very familiar with checking input arguments and ensuring robustness of functions could you point me to a guide on how to approach this problem in Matlab?
Thank you very much for your Support and time!
Adam
Adam 2015-9-21
Oops, sorry, I messed up the syntax with some copy and pasting there. Yes, your version is what I intended!
I haven't seen any guides on this really, I just refer regularly to the validateattributes help page in the Matlab documentation.
I find it invaluable personally to use this in almost every public function I write (I use an OOP style, but it is just the same as function programming from that perspective) so I have just had a lot of practice with using this and writing my own validation wrapper functions occasionally for e.g. validating an axes as being valid and of the correct type, etc.
I've rarely actually needed to validate contents of a cell array like that, but I have also used cellfun quite a lot so practice brings a certain degree of familiarity with doing this.
Since we use so many validateattributes calls I have never bothered with the extra arguments to tell me the function name, argument name and number etc. They just feel like too much clutter for me since the error message hyperlink takes me to the relevant validateattributes line (or a debug stop on errors) and I can easily see from there.
It does give a neater error message in general, but we have ~1500 occurrences of validateattributes in our code repository so adding those extra arguments is just too much typing (plus I rename variables often which adds extra work too there)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by