Staying at the structs has some benefits if you use dynamic field names:
param.eps = 6;
param.beta = 0.99;
param.theta = 0.7532;
param.gamma = 0.5;
fields = fieldnames(param);
for i = 1:length(fields)
aField = fields{i};
fprintf('%s = %g\n', aField, param.(aField));
end
If you assign the variable "eps" dynamically, you will overwrite the built-in function with the same name. Such tricks will impede the debugging dramatically!
But if you really have a really good reason to need this really:
function FieldToVar(S)
F = fieldnames(S)
for i = 1:length(F)
aF = F{i};
assignin('caller', aF, S.(aF));
end
This is faster than an equivalent EVAL approach and less dangerous. Nevertheless, I think, this method is worse than using the dynamic field names directly.
Best Answer