MATLAB: How to change the value of a field of a structure in MATLAB 7.8 (R2009a)

functionMATLABscriptstructure

I have MATLAB script and MATLAB function. I want to change the value of structure fields in my code. The structure variable exists in base workspace.

Best Answer

To change the value of structure field in a script, you can directly set the value as a script operates on the base workspace. Thus, one can directly specify the 'structure_name.field_name' in the script and update the value:
% in a script:
structure_name.field_name = value;
To change the value of a structure field in a MATLAB function file, use the EVALIN function:
% in a function:
evalin('base', 'structure_name.field_name = value');
This is because the function has its own stack space. It does not use base workspace.
Related Question