是否有可能在MATLAB中强制输入参数数据类型? [英] Is it possible to enforce input argument data types in MATLAB?

查看:140
本文介绍了是否有可能在MATLAB中强制输入参数数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保用户定义的MATLAB函数(包含在m文件中)的输入参数具有某种类型。我明白,MATLAB自动将数据类型分配给变量(以满足某些人的喜好和对其他人的沮丧),但是我想知道在MATLAB中是否存在严格数据类型的选项,或者类似的东西用于用户定义函数的输入参数。



我在这两个网页上找到了有关MATLAB的基本类(数据类型)的有用说明:



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



然而, ,我一直无法找到严格数据类型问题的答案,特别是对于函数输入参数。我认为这将是一个非常基本的问题,在很多地方已经得到了回答,但经过广泛的搜索后,我还没有找到确凿的答案。现在,我一直在使用 is [TYPE]()函数手动检查数据类型,如果它不符合,则显示一条错误消息,虽然这看起来很潦草,而我希望我能让MATLAB为我执行它。



下面是一个函数的例子,我想在其中指定输入参数数据类型。它位于MATLAB当前路径中的 strict_data_type_test.m 文件中。在这个函数中,我想强制变量 yes_or_no 为MATLAB的逻辑数据类型。我知道我可以使用 islogical()函数手动检查,但是我的问题是,是否有可能让MATLAB为我执行它。我也知道任何非零的double值都为true,零值为false,但我想强制用户发送逻辑以确保错误的参数是例如,不是偶然发送的。以下是示例函数:

 函数y = strict_data_type_test(x,yes_or_no)

%手动数据类型检查可以在这里,但手动检查不可取

if(yes_or_no)
y = 2。* x;
else
y = -5。* x;
end

end

在输入前添加数据类型参数变量名称(与大多数编程语言一样)将数据类型文本视为另一个变量名称,而不是数据类型标识符。从这个角度来看,严格的数据类型在MATLAB中无法用任何方式实现,但也许你们中有一个知道了一个我无法找到的有用的技巧,功能或语法。



预先感谢您的帮助。

解决方案

不能只挑选一个作为接受的答案,而是要总结我迄今从你身上学到的东西:


  • <强大>否,MATLAB没有为函数输入参数内置严格数据类型。
  • MATLAB在运行之前编译代码,因此手动验证检查不应该是非常重要的性能(分析器可用于评估此功能)

  • 存在许多有用的手动验证检查方法,这里按照与我正在尝试做的最不相关的最相关的顺序列出:



    其他想法:


    • 看来 inputParser class是以最专业的方式验证输入参数的总体共识。在一个相关的(但不是重复的)stackoverflow帖子中指出,新的MathWorks函数倾向于使用这个类,这表明它可能是最好的和最新的选择。
    • 由于MathWorks提供的MATLAB函数似乎没有强制执行严格的输入参数数据输入,这进一步表明,甚至如果有可能这样做,它可能不是一个推荐的方法。
    • MATLAB似乎认为错误处理和异常处理是两个不同的概念。例如,下面是两个指向MATLAB文档中心的链接,它们显示了MathWorks如何以不同的方式考虑错误处理和异常处理: MathWorks文档中心关于错误处理的文章MathWorks文档中心关于异常处理的文章。有关此主题的相关StackOverflow帖子已发布,可在此处(链接)找到。我联系了MathWorks并在该文章中添加了关于此主题的一些新信息,因此如果您有兴趣,可以通过以下链接阅读更多内容。


    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.

    Thank you in advance for your help.

    解决方案

    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:
    • 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.

    这篇关于是否有可能在MATLAB中强制输入参数数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆