MATLAB: Can I create local function variables at runtime and are how are they evaluated subsequently

anonymouslocalMATLABnestedobjectorientedprogrammingshadow

I am using a parent function that calls another child (nested) function. This child function creates a new variable inside the parent's local workspace, using "assignin('caller', 'newVariable')". How can I use this "newVariable" in the parent function's body to evaluate it?

Best Answer

In order to use the dynamically created variable in the parent function, you need to do EITHER:
1) directly type the variable name in the code to match the name created and runtime. Ensure you are not shadowing any builtin variables, objects or functions. Otherwise the coded evaluation of the variable will be parsed at the beginning of runtime and the shadowed variable will be used instead.
OR
2) use dynamic name generation, like shown in these examples: