How can i create constant in a .m archive and use it for the creation of new objects?

5 ビュー (過去 30 日間)
Hi, I have an archive named Constantes.m
%% Logica
TRUE=1;
FALSE=0;
%% Colores
AZUL=[0 0 1];
ROJO=[1 0 0];
BLANCO=[1 1 1];
%% Tablero
COLUMNAS=8;
FILAS=8;
%% Juego
TURNO_IA=[1 0 0];
TURNO_JUGADOR=[0 0 1];
And i want to use those variables into my class archives, for example
classdef Pieza
properties
Color=BLANCO;
Rey=FALSE;
Direccion=0;
end
methods
function obj = Pieza(Color,ColorDeJugador)
%Constructor de Pieza (Color, Color Elegido)
obj.Color=Color;
Rey=FALSE;
if isequal(Color,ColorDeJugador)
Direccion=-1;
elseif isequal(Color,BLANCO);
Direccion=0;
else
Direccion=1;
end
end
function obj = Coronar(obj)
%Parametro del objeto Pieza Rey a 1(verdadero)
obj.Rey=TRUE;
end
end
end
how is it done?
Thanks

採用された回答

Walter Roberson
Walter Roberson 2020 年 12 月 3 日

その他の回答 (1 件)

Rik
Rik 2020 年 12 月 3 日
編集済み: Rik 2020 年 12 月 3 日
You could turn your script into a function. If you store all variables as fields of a struct you can use something like this to select a variable:
Color=getConstant('BLANCO');
In getConstant.m:
output=constants.(input);
This way you can even take advantage of persistent variables to generate the struct only once.

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by