user1876942
user1876942

Reputation: 1501

Set Matlab function parameter as uint8

Is it possible in Matlab to say what the function expects? something like this:

function functionA( obj,  uint8(param) )

Here I am saying that the function expects one parameter of type uint8.

Upvotes: 1

Views: 69

Answers (2)

Mohammadreza Khoshbin
Mohammadreza Khoshbin

Reputation: 329

To complement Rody's answer, there are four ways that you can do this:

  1. Use a conditional and raise an exception if the argument is not of the expected type. The problem with this method is that you have to write a lot of code.
  2. Use an assertion. See Rody's answer or here. One can argue that this is not what assertions are supposed to be used for, but you can certainly use them this way.
  3. Use the validateattributesfunction. See here. This is a very good balance between simplicity and utility. It allows you to check for a number of properties in an argument (and generally, any variable at any part of code)
  4. Use the inputParser class. See here. This is the most powerful method of parsing inputs, but may be overkill. Also, the cost of creating an inputParser object means that it may not be a good idea for functions that are called repeatedly. Nevertheless, it's very good for the public API.

Upvotes: 2

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

Not on the function signature. Typically, you do this via an assert block:

function (obj, param)

    assert(isa(param, 'uint8'),...
           [mfilename ':invalid_datatype'],...
           'Parameter ''param'' must be of class ''uint8''; received ''%s''.',...
           class(param));

Upvotes: 6

Related Questions