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

2 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Dic. de 2020

Más respuestas (1)

Rik
Rik el 3 de Dic. de 2020
Editada: Rik el 3 de Dic. de 2020
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.

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by