我想在函数范围内本地定义枚举和常量。
我看到 MATLAB 提供枚举和常量作为其面向对象编程框架的一部分。但是,如果您尝试在函数范围内定义它们,它们将不起作用。例如,如果您尝试以下操作,MATLAB 会抱怨“解析错误:无效语法”:
function output = my_function(input)
classdef my_constants
properties (Constant)
x = 0.2;
y = 0.4;
z = 0.5;
end
end
classdef colors
enumeration
blue, red
end
end
statements;
原因似乎是每个都classdef
需要在自己的.m
文件中定义。
我想避免.m
为我使用的每个枚举或一组常量创建一个文件。有没有办法做到这一点?我有哪些选择?
附录 1:
正因为有人问我一个例子,这里是一个伪代码。这个例子描述了我定义和使用本地枚举的需要。
假设我有一个枚举类型colors
,它可以是RED
or BLUE
。我想colors
在我的函数中本地定义,并使用它来控制我在函数中的语句流:
function output = my_function(input)
# ....
# Code that defines the enumeration 'colors'
#....
my_color = colors;
# ... code that changes 'my_color' ...
switch my_color
case RED
do this
case BLUE
do that;
end
附录 2:
我可以通过利用 Java 代码来做到这一点吗?如果是这样,怎么做?