MATLAB中的常数 [英] Constants in MATLAB

查看:203
本文介绍了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天全站免登陆