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

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Walter Roberson
Walter Roberson il 3 Dic 2020

Più risposte (1)

Rik
Rik il 3 Dic 2020
Modificato: Rik il 3 Dic 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.

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by