matlab struct initialization issue

3 次查看(过去 30 天)
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.

采纳的回答

Ameer Hamza
Ameer Hamza 2020-11-4
编辑:Ameer Hamza 2020-11-4
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 个评论
per isakson
per isakson 2020-11-4
See matlab.lang.makeValidName and isvarname()
Ameer Hamza
Ameer Hamza 2020-11-4
@Anmol Pardeshi, yes. The link mentions the rules to define a fieldname.
@Per Isakson, Thanks for the information.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by