validateattributes() - content of a cell

10 visualizaciones (últimos 30 días)
Marius Mueller
Marius Mueller el 18 de Sept. de 2015
Comentada: Adam el 21 de Sept. de 2015
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?

Respuesta aceptada

Adam
Adam el 18 de Sept. de 2015
Editada: Adam el 18 de Sept. de 2015
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 comentarios
Marius Mueller
Marius Mueller el 21 de Sept. de 2015
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 el 21 de Sept. de 2015
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)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by