What is defined as an integer type, when calling validateattributes?

5 visualizaciones (últimos 30 días)
Hi,
since
isinteger(int8(5))
is true, I would expect
function c = minimalExample(a)
p = inputParser;
addRequired(p,'a',@(x)validateattributes(x,{'integer'}, {'positive'}));
parse(p,a)
c = a;
end
to return
ans = int8 5.
when calling minimalExample(int8(5)). But it does not. It returns:
The value of 'a' is invalid. Expected input to be one of these types:
integer
Instead its type was int8.
However
function c = minimalExample(a)
p = inputParser;
addRequired(p,'a',@(x)validateattributes(x,{'int8','int16', 'int32', 'int64', 'uint8','uint16', 'uint32', 'uint64'}, {'positive'}));
parse(p,a)
c = a;
end
when calling minimalExample(int8(5)) works fine. What in the name of Knuth is an 'integer' type? Thanks in advance :).

Respuesta aceptada

Stephen23
Stephen23 el 10 de Sept. de 2019
Editada: Stephen23 el 10 de Sept. de 2019
Lets read the validateattributes documentation, and see what it says.
All of its calling syntaxes start with the exactly same three arguments:
validateattributes(A,classes,attributes,...)
The 'integer' option is listed in the section for the third input argument attributes:
I do not see 'integer' anywhere in the section for the second input argument classes.
But you used 'integer' for the second input argument, which as far as I can tell from reading the documentation, is not supported (it would be nice if it threw an error when that occurs...).
"What in the name of Knuth is an 'integer' type?"
Nothing. It doesn't exist.
I suspect you want something like this:
validateattributes(x,{'numeric'}, {'integer','positive'})

Más respuestas (1)

Steven Lord
Steven Lord el 10 de Sept. de 2019
The word 'integer' is a slightly overloaded term in MATLAB. Whether it's appropriate for your purposes to use 'integer' in the third input to validateattributes and/or a cell array of the eight integer type names in the second input depends on what you want to allow. The following validateattributes calls succeed.
>> validateattributes(int8(5), {'numeric'}, {'integer', 'positive'})
>> validateattributes(5, {'numeric'}, {'integer', 'positive'})
Both the int8 scalar and the double scalar are numeric and contain an integer value. The following fails:
>> validateattributes(pi, {'numeric'}, {'integer', 'positive'})
Expected input to be integer-valued.
If you wanted to restrict the validateattributes call to only allow integer types you need to specify some or all of the eight types (which each have the same name as their conversion function) in the second input. This succeeds:
>> validateattributes(int8(5), {'int8'}, {'integer', 'positive'})
This fails:
>> validateattributes(5, {'int8'}, {'integer', 'positive'})
Expected input to be one of these types:
int8
Instead its type was double.
If you restrict the types to be a subset of the eight integer types, the values must be integer values -- those types cannot contain non-integer values. If you try, MATLAB makes them integer values.
>> int8(pi)
ans =
int8
3
But you can store integer values in a non-integer type.
  1 comentario
Eric Schöneberg
Eric Schöneberg el 10 de Sept. de 2019
Thanks a lot. Since MATLAB is a 'dynamic type'-programming languate, I will only check for integer value, not integer type. I will not ask the programmer to specify the type manually, but simply want to make sure the function does not return unexpected results:
validateattributes(a, {'numeric'}, {'integer', 'positive'})
Once using C Mex, typecasting will be necessary anway. Obviously typecasting e.g.
int8(pi)
int8('bla')
is possible, but not necessarily wise. Typechecking in MATLAB seems more easy-going than doing so in C.

Iniciar sesión para comentar.

Categorías

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

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by