Matlab中类库的全局变量 [英] Global variables for class library in matlab
问题描述
我声明了几个matlab
类.我如何声明所有类中都可见的常量?
例如:这些常量可以是在所有类的方法中使用的物理常量.首先想到的是使用全局变量.有什么更好的办法吗?最好在一个单独的文件中声明这些常量.
I have several matlab
classes declared. How could I declare constants which are seen in all classes?
For instance: these constants can be physical constants which are used in methods of all classes. The first thing coming to mind is using global variables. Is there any better way? It will be nice to declare these constants in a separate file.
推荐答案
包含常量的类是一种很好的干净方法.请参阅Matlab文档中的文章: http://www.mathworks.com/help /matlab/matlab_oop/properties-with-constant-values.html
A class containing the constants is a nice clean way to do this. See the article in Matlab documentation: http://www.mathworks.com/help/matlab/matlab_oop/properties-with-constant-values.html
例如,如果您创建一个名为NamedConst
的类,如下所示:
For example, if you create a class called NamedConst
as follows:
classdef NamedConst
properties (Constant)
R = pi/180;
D = 1/NamedConst.R;
AccCode = '0145968740001110202NPQ';
RN = rand(5);
end
end
您可以通过以下方式引用值
You can reference values with
radi = 45*NamedConst.R
您可以在给定的链接中找到更多详细信息.
You can find more details in the link given.
这篇关于Matlab中类库的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!