MATLABda mahalliy raqamlar va sobit belgilash

Funktsiya doirasida mahalliy raqamlar va barqarorlarni aniqlashni istayman.

Men MATLABning ob'ektga asoslangan dasturlash tizimining bir qismi sifatida raqamlar va doimiylarni taqdim etganini ko'rdim. Biroq, ularni funktsiya doirasida belgilashga harakat qilsangiz, ular ishlamaydi. Masalan, MATLAB quyidagi xatoliklarni ko'rib chiqsangiz, "Parse error: invalid syntax" bilan shikoyat qiladi:

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;

Buning sababi shundaki, classdef .m faylida aniqlanishi kerak.

Men foydalanadigan har bir numaralandırma yoki barqaror majmui uchun .m fayliga ega bo'lishdan qochmoqchiman. Buning bir yo'li bormi? Mening variantlarim nima?

1-ilova:

Sine menga namuna so'radi, bu erda pseudocode . Ushbu misol, mahalliy raqamlarni aniqlash va ulardan foydalanishga bo'lgan ehtiyojimni aks ettiradi.

RED yoki BLUE bo'lishi mumkin bo'lgan ranglar deb nomlangan ro'yxatdan ro'yxatga ega ekanligimni ayting. Funktsiyadagi mahalliy kodni colors ni belgilashni xohlayman va bu funksiyadagi so'zlarimning oqimini nazorat qilaman:

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-qo'shimcha:

Java kodini qo'llash orqali buni qila olamanmi? Agar shunday bo'lsa, qanday qilib?

1
Siz o'zingiz istagan narsangizga misol keltira olasizmi?
qo'shib qo'ydi muallif Jacob, manba

1 javoblar

O'ylaymanki, sonlar juda ko'p. Siz buni amalga oshirishingiz mumkin

  • 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
    

Umid qilamanki bu yordam. Agar iltimos qilmasangiz, iltimos.

1
qo'shib qo'ydi