matlab获取全局变量的值,Matlab中类库的全局变量

本文探讨如何在Matlab中避免使用全局变量,推荐通过创建专门的类来存储并访问物理常数。通过实例化`NamedConst`类,可以方便地在所有类中复用这些常数,提高代码组织性和可维护性。链接提供了详细步骤和例子。
摘要由CSDN通过智能技术生成

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.

解决方案

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值