monarch
monarch

Reputation: 11

Struct field name by referencing to a value in MATLAB

I am currently working on a struct in MATLAB and have a question regarding this.

Let us say i have declared a struct: structVariable=struct('abc',[],'cde',[])

i.e. structVariable =

abc: []
cde: []

Further I have a char variable,

charVariable='abc';

Now, I am trying to use structVariable.abc with something like

structVariable.charVariable =5;

but this does not work. Is it possible to reference to the value of charVariable with something like &charVariable as in c++ ?

Upvotes: 1

Views: 91

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

This seems to be the easiest way:

structVariable.(charVariable) = 5;

Upvotes: 1

Luis Mendo
Luis Mendo

Reputation: 112659

To set the field value:

setfield(structVariable,charVariable,5)

To get the field value:

getfield(structVariable,charVariable)

Upvotes: 0

Related Questions