MATLAB: Dynamic structure reference gives error

dynamic structure reference

I have s structure field named d.NL330260 and a temporary variable grade_n which is a 1×1 cell array containing 'NL330260'. I tried implementing the following:
grade_n = 'NL330260';
d.(grade_n)
but , I get this error message.
Argument to dynamic structure reference must evaluate to a valid field name.
What could be the problem??

Best Answer

This is not the exact code you were using:
grade_n = 'NL330260';
d.(grade_n)
It was more like:
grade_n = {'NL330260'};
d.(grade_n)
When using dynamic field names, the dynamic field name must be a char row vector. You were passing in a scalar cell array with a char row vector inside the cell, but that's different. Using char as you did is one way to resolve the problem; another is to extract the char row vector from the cell.
grade_n = {'NL330260'};
d.(grade_n{1})