Matlab中类库的全局变量 [英] Global variables for class library in matlab

查看:362
本文介绍了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屋!

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