MATLAB 中的常量 [英] Constants in MATLAB

查看:15
本文介绍了MATLAB 中的常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经拥有了一堆 MATLAB 代码,并注意到代码中散布着一堆神奇的数字".通常,我喜欢用 C、Ruby、PHP 等语言制作这些常量.当谷歌搜索这个问题时,我发现拥有常量的官方"方式是定义返回常量值的函数.看起来很笨拙,特别是因为当每个文件允许多个函数时,MATLAB 可能会很挑剔.

I've come into ownership of a bunch of MATLAB code and have noticed a bunch of "magic numbers" scattered about the code. Typically, I like to make those constants in languages like C, Ruby, PHP, etc. When Googling this problem, I found that the "official" way of having constants is to define functions that return the constant value. Seems kludgey, especially because MATLAB can be finicky when allowing more than one function per file.

这真的是最好的选择吗?

Is this really the best option?

我很想使用/制作类似 C 预处理器的东西来为我做这件事.(我发现名为 mpp 的东西是由其他人在类似的困境中制作的,但它看起来已被废弃.代码无法编译,我不确定它是否能满足我的需求.)

I'm tempted to use / make something like the C Preprocessor to do this for me. (I found that something called mpp was made by someone else in a similar predicament, but it looks abandoned. The code doesn't compile, and I'm not sure if it would meet my needs.)

推荐答案

我通常只是用 UPPER_CASE 定义一个变量并放在文件顶部附近.但你必须承担不改变其价值的责任.

I usually just define a variable with UPPER_CASE and place near the top of the file. But you have to take the responsibly of not changing its value.

否则您可以使用 MATLAB 类定义命名常量.

Otherwise you can use MATLAB classes to define named constants.

这篇关于MATLAB 中的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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