Use `assignin` to assign a variable by name within a function.

Alec Jacobson

January 11, 2014

weblog/

The assignin function of matlab is useful for assigning variables by name. However, by default you can only assign variables living in the base workspace (i.e. the IDEs workspace) and the "caller" workspace where caller is the caller not of assignin but rather the caller of the function calling assignin. This is kind of weird. I wanted to use assignin to assign variables by string in the current workspace. To do this I have a little trick:

feval(@()assignin('caller','name',value);

This temporarily creates an anonymous function around assignin and then calls it. Effectively making your current workspace the "caller".