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

5 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 3 Dec 2020

More Answers (1)

Rik
Rik on 3 Dec 2020
Edited: Rik on 3 Dec 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.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!