Defining enumerations and constants locally in MATLAB

心已入冬 提交于 2019-12-11 11:57:45

问题


I would like to define enumerations and constants locally within the scope of a function.

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;

The reason seems to be that each classdef needs to defined in its own.m file.

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?

Addendum 1:

Sine I was asked for an example, here's one in pseudocode. This example depicts my need for defining and using local enumerations.

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

Addendum 2:

Could I do this by leveraging Java code? If so, how?


回答1:


I think enumerations would be overkill. You can do this by

  • 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.



来源:https://stackoverflow.com/questions/7931154/defining-enumerations-and-constants-locally-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!