I created this very useful bit of code to assign variables dynamically from a struct :
function getParam(param)
% this function extracts the fields of structure param and assigns them
% to variables of corresponding names in the caller workspace
allFieldsParam = fieldnames(param);
for iField = 1:length(allFieldsParam)
assignin('caller',allFieldsParam{iField},param.(allFieldsParam{iField}));
end
The problem is that when I call getParam within a function, sometimes it works and sometimes it returns an error of the form :
??? Error using ==> assignin
Attempt to add "blocksizes" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables
for details.
Error in ==> getParam at 7
assignin('caller',allFieldsParam{iField},param.(allFieldsParam{iField}));
Error in ==> classif_nmf_db at 15
getParam(param);
Anyone has a clue how I can fix this ?
Regards
AL
This is to enforce a good programming practice in MATLAB.
The document you are referred to is located here:
Nested functions: Restrictions on Assigning to Variables
Loren also has a blog entry about it.
UPDATE
Have a look on this File Exchange submission:
Pack & Unpack variables to & from structures with enhanced functionality (v2struct).