在MATLAB中本地定义枚举和常量 [英] Defining enumerations and constants locally in MATLAB
问题描述
我想在函数范围内局部定义枚举和常量.
I would like to define enumerations and constants locally within the scope of a function.
我看到MATLAB作为其面向对象的编程框架的一部分提供了枚举和常量.但是,如果尝试在函数范围内定义它们,则它们将不起作用.例如. MATLAB抱怨解析错误:语法无效".如果您尝试以下操作:
I saw that MATLAB provides enumerations and constants as part of its object-oriented programming framework. However, if you try to define them within the scope of a function, they don't work. E.g. MATLAB complains with "Parse error: invalid syntax" if you try the following:
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
文件中定义.
The reason seems to be that each classdef
needs to defined in its own.m
file.
我想避免使用的每个枚举或常量集都有一个.m
文件.有没有办法做到这一点?我有什么选择?
I would like to avoid having an .m
file for every enumeration or set of constants that I use. Is there a way to do this? What are my options?
请教我一个例子,这是伪代码中的一个例子.这个例子说明了我对定义和使用本地枚举的需求.
Sine I was asked for an example, here's one in pseudocode. This example depicts my need for defining and using local enumerations.
说我有一个名为colors
的枚举类型,可以是RED
或BLUE
.我想在函数中本地定义colors
,并使用它来控制函数中语句的流向
Say I have an enumeration type called colors
that can be RED
or BLUE
. I would like to define colors
locally in my function, and use it do control the flow of my statements in the function:
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代码来做到这一点吗?如果可以,怎么办?
Addendum 2:
Could I do this by leveraging Java code? If so, how?
推荐答案
我认为枚举是过大的.您可以通过
I think enumerations would be overkill. You can do this by
- 定义RGB值的matlab结构
- 确定输入"哪种颜色并记住该颜色字段名称
-
用这种颜色做某事
- defining a matlab struct of RGB values
- determine which color is "inputted" and remembering that color fieldname
do something with that color
function output = my_function(input)
% Code that defines the enumeration 'colors' in terms of RGB
colors.RED = [1 0 0];
colors.BLUE = [0 0 1]
... etc ...
% ... here... what determine my_color is,
% which is logic specific to your function
%
% You should assign 'my_color' to the same struct
% fieldname used in the above 'colors'
if( some red conditon )
my_color = 'RED';
elseif( some blue condition)
my_color = 'BLUE';
elseif(etc...)
end
% at this point, my_color will be a fieldname
% of the 'colors' struct.
%
% You are able to dynamically extract the
% RGB value from the 'colors' struct using
% what is called called dynamic field reference.
%
% This means...
%
% While you can hardcode blue like this:
%
% colorsStruct.BLUE
%
% You are also able to dynamically get BLUE like this:
%
% colorName = 'BLUE';
% rgbValue = colorsStruct.(colorName);
%
%
% Loren has a good blog on this:
%
% http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/
% Extract the rgb value
my_color_rgb_value = colors.(my_color);
% Do something with the RGB value
your_specific_function(my_color_rgb_value);
end
希望这会有所帮助.如果没有,请发布后续消息.
Hope this helps. Please post a follow up if not.
这篇关于在MATLAB中本地定义枚举和常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!