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.
function types matlab
Babak
source share