validateattributes() - content of a cell
8 次查看(过去 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?
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!