matlabfunctiontypes# Is it possible to enforce input argument data types in MATLAB?

I would like to ensure that the input arguments to a user-defined MATLAB function (contained in an m-file) are of a certain type. I understand that MATLAB automatically assigns data types to variables (to the liking of some and the dismay of others), but I would like to know if there is an option of "strict data typing" in MATLAB, or something of the sort, especially for input arguments for a user-defined function.

I found a helpful explanation of MATLAB's "fundamental classes" (data types) at these two webpages:

http://www.mathworks.com/help/matlab/matlab_prog/fundamental-matlab-classes.html http://www.mathworks.com/help/matlab/data-types_data-types.html

However, I have been unable to find an answer to the question of strict data typing, particularly for function input arguments. I thought it would be a pretty basic question that already had been answered in numerous places, but after extensive searching I have not yet found a conclusive answer. For now, I have been manually checking the data type using the `is[TYPE]()`

functions and displaying an error message if it does not comply, though this seems sloppy and I wish I could just get MATLAB to enforce it for me.

Below is an example of a function in which I would like to specify the input argument data type. It resides in a file called `strict_data_type_test.m`

in MATLAB's current path. In this function, I would like to force the variable `yes_or_no`

to be of MATLAB's `logical`

data type. I know I can use the `islogical()`

function to manually check, but my question is if it is possible to have MATLAB enforce it for me. I also know that any non-zero double evaluates to true and a zero evaluates to false, but I want to force the user to send a `logical`

to make sure the wrong argument was not sent in by accident, for example. Here is the example function:

```
function y = strict_data_type_test( x, yes_or_no )
% manual data type check can go here, but manual check is not desirable
if (yes_or_no)
y = 2 .* x;
else
y = -5 .* x;
end
end
```

Adding the data type before the input argument variable name (like in most programming languages) treats the data type text as another variable name instead of a data type identifier. From that it would seem that strict data typing is not possible in MATLAB by any means, but maybe one of you many gurus knows a useful trick, feature, or syntax that I have not been able to find.

Solution

I've gotten some great responses so I can't pick just one as the "accepted answer", but to summarize what I've learned from you all so far:

**No, MATLAB does not have built-in strict data typing for function input arguments**- MATLAB compiles the code before running, so manual validation checking should not be very taxing on performance (the profiler can be used to assess this)
- Many helpful methods of doing the manual validation checking exist, listed here in order of most relevant to least relevant for what I was trying to do:
`inputParser`

class`validateattributes()`

- Error/exception handling (
`throw()`

,`error()`

,`assert()`

, etc.) - MATLAB's built-in state detection functions (a.k.a predicate functions)

- I can look through some MathWorks-provided MATLAB functions (or Statistics toolbox functions) for ideas on how to validate input arguments by typing
`edit`

followed by the function name. Two suggested functions to look at are`normpdf()`

(from the Statistics toolbox) and`integral()`

. Some other functions I found helpful to look at are`dot()`

and`cross()`

.

Other thoughts:

- It would appear that the
`inputParser`

class was the overall concensus on the most professional way to validate input arguments. It was noted on a related (but not duplicate) stackoverflow post that the newer MathWorks functions tend to make use of this class, suggesting that it may be the best and most up-to-date choice. - Since the MathWorks-provided MATLAB functions do not appear to enforce strict input argument data typing, this further suggests that even if it was possible to do so, it may not be a recommended approach.
- MATLAB seems to regard "error handling" and "exception handling" as two different concepts. For example, here are two links to MATLAB's Documentation Center that show how MathWorks considers "error handling" and "exception handling" differently: MathWorks Documentation Center article on Error Handling, MathWorks Documentation Center article on Exception Handling. A relevant StackOverflow post has been made on this topic and can be found here (link). I contacted MathWorks and added some new information about this topic to that post, so if you are interested you may read more by following that link.

- how to reshape a cell array to matrix in desired size
- Using Mat_CreateVer for MATLAB versions higher than 7.3
- Making a numpy ndarray matrix symmetric
- Matlab 2021 and2023 cross compatability
- How to plot figures at the end of the for loop MATLAB?
- Variable transfer from App Designer to general .m script file
- Anomalous behavior of the Simulink signal - Simscape
- What is the fastest method to searsch for a part of a matrix inside of a large matrix?
- Can I Prevent Git from Using XTerm Escape Sequences?
- Use VPA to evaluate tiny probabilities
- Generate Array of Random Numbers with Specified Standard Deviation and Mean
- Matlab update plot with multiple data lines/curves
- Matlab vectorize for loop (grid depends on loop index)
- How to use a flag variable to break all the for-loops in spmd statement?
- How to produce the indexes from pdist2 function in Octave?
- Calculation of sensitivity and specificity from confusion matrix
- How do I read comma separated values from a .txt file in MATLAB using textscan()?
- How do you code Garson's algorithm in matlab to find the relative importance of parameters when training a neural network model?
- add data label to a grouped bar chart in matlab
- Using 1D convolutional with same shape so it will work with FFT?
- super fast median of matrix in OpenCV (as fast as MATLAB)
- How to create a struct or type from a dictionary in Julia?
- MATLAB Delay an Input Signal
- Is there an Octave equivalent of Matlab's `contains` function?
- Matlab define callback function for mouse click on a biograph
- getting a return value from a function in MATLAB
- How to convert nifti file to AFNI file?
- load .mat file, but not on the workspace
- Fast plane fitting to many points
- Cubic spline interpolation vs polynomial interpolation