matlab struct initialization issue

I'm trying to initialize a strut which would have fields that represent age groups like <39, 40-49, 50-59 and so on. So I tried the following
x = struct('below39',{},'AG 40 to 49',{}); but it gives me an invalied field name error. I also tried
x = struct('below39',{}); and that worked. I'm not sure why is this an issue. Any insights would eb helpful.

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 4 de Nov. de 2020
Editada: Ameer Hamza el 4 de Nov. de 2020
In MATLAB, the struct fieldnames cannot have spaces. The following will work
x = struct('below39',{},'AG_40_to_49',{})
Read here about rules to for creating a fieldname: https://www.mathworks.com/help/matlab/ref/struct.html#d122e1258552

3 comentarios

Anmol Pardeshi
Anmol Pardeshi el 4 de Nov. de 2020
Thanks Ameer. Although one more thing worth noting is that the first character must be a alphabet and not number. In that light, '40_to_49' would not work. It should start with a alphabet.
per isakson
per isakson el 4 de Nov. de 2020
See matlab.lang.makeValidName and isvarname()
Ameer Hamza
Ameer Hamza el 4 de Nov. de 2020
@Anmol Pardeshi, yes. The link mentions the rules to define a fieldname.
@Per Isakson, Thanks for the information.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 4 de Nov. de 2020

Comentada:

el 4 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by