Can I force input argument data types in MATLAB?

I would like to make sure that the input arguments of the user-defined MATLAB function (contained in the m file) are of a specific type. I understand that MATLAB automatically assigns data types to variables (according to some, and others are confused), but I would like to know if there is an option to "strictly print data" in MATLAB or something like that, especially for input arguments to a user-defined function .

I found a useful explanation of MATLAB's "fundamental classes" (data types) on these two web pages:

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 could not find the answer to the question about a strict data set, especially for function input arguments. I thought it would be a fairly simple question, which has already been answered in many places, but after extensive searches I have not yet found a definitive answer. At the moment, I manually checked the data type using the is[TYPE]() functions and displayed an error message if it does not match, although this seems to be messy, and I would just like to get MATLAB to ensure its execution for me.

The following is an example of a function in which I would like to specify the data type of the input arguments. It is located in a file named strict_data_type_test.m in the current MATLAB path. In this function, I would like to force the yes_or_no variable yes_or_no have the MATLAB logical data type. I know that I can use the islogical() function to check manually, but my question is whether it is possible for MATLAB to provide it for me. I also know that any non-zero double result is true and zero is false, but I want to force the user to send logical to make sure that the wrong argument was not sent by accident, for example. Here is an 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 a data type to the variable name of the input argument (as in most programming languages) considers the text of the data type as a different variable name instead of the data type identifier. From this, it would seem that strict data entry is impossible in MATLAB by any means, but perhaps one of you, many gurus, knows a useful trick, function or syntax that I could not find.

Thank you in advance for your help.

+7
function types matlab
source share
4 answers

I have excellent answers, so I cannot choose one of them as the “accepted answer”, but to summarize what I have learned from you so far:

  • No, MATLAB does not have built-in strict data printing for function input arguments
  • MATLAB compiles the code before running, so manual validation should not be heavily taxed on performance (the profiler can be used to evaluate this)
  • There are many useful manual validation verification methods listed here, in order of most relevance to the smallest value for what I tried:
  • I can look at some MATLAB functions provided by MATLAB (or statistics toolkit functions) for ideas on how to check input arguments by typing edit and then the function name. Two suggested functions: normpdf() (from the Statistics toolbar) and integral() . Some other functions that I found useful for viewing are dot() and cross() .

Other thoughts:

  • It seems that the inputParser class was a general consensus on the most professional way of checking input arguments. It has been noted at https://stackoverflow.com/a/969860/ ... that new MathWorks features tend to use this class, suggesting that it might be the best and most-time choice.
  • Since the MATLAB functions provided by MathWorks do not force the input of strict input data, this once again indicates that even if it were possible, this might not be the recommended approach.
  • MATLAB seems to consider “error handling” and “exception handling” as two different concepts. For example, here are two links to the MATLAB Documentation Center that show how MathWorks treats “error handling” and “exception handling” differently: The MathWorks Error Center Documentation article, the MathWorks Documentation Center article on exception handling . The corresponding StackOverflow entry was made on this topic and can be found here (link) . I contacted MathWorks and added new information about this topic to this post, so if you are interested, you can read more by following this link.
+3
source share

validateattributes may also work for you if there is a corresponding attribute for your case. For example, if you want to ensure that yes_or_no is a logical scalar, you can try:

 validateattributes(yes_or_no,{'logical'},{'scalar'}) 

Otherwise, there may be a non-empty attribute.

+5
source share

Perhaps you write code that is tedious or worried that it degrades performance:

 if ~islogical(yes_or_no) && ~isscalar(yes_or_no) error('test:NotLogicalType','Second argument must be logical (Boolean).'); end if yes_or_no y = 2 .* x; else y = -5 .* x; end 

Recall, however, that Matlab compiles the code before running, so even if you need to check many conditions, it will be pretty fast. Run the profiler to see.

Another option in some cases (maybe not your example) is to use the lazier method. This option allows your code to work with any inputs, but uses a try / catch to catch any error:

 try if yes_or_no y = 2 .* x; else y = -5 .* x; end catch me ... error('test:NotLogicalType','Second argument must be logical (Boolean).'); % rethrow(me); end 

The above code throws an error if, for example, yes_or_no was an array of cells (it will still allow non-boolean, non-scalar, etc. values ​​for yes_or_no , but Matlab is often overly permissive). Then you can create your own error message, determine which error was selected, try something else, etc. Many functions in the Statistics toolbar use this approach (for example, type edit normpdf in the command window) for better or worse.

+1
source share

Matlab provides an "input parser" that allows you to validate input. In addition to this, you can use the statements:

 assert(islogical(yes_or_no),'logical input expected') 

To ensure the correct number of input arguments, use narginchk .

btw: check out some Matlab features like edit integral and see how this works with tmw.

0
source share

All Articles