I have a program that requires passing a large number of variables to a function. Rather than have a long list of global variables, I'd rather pack them into a struct, declare the struct as global, then unpack the struct in the function workspace. Obvously, a requirement is that the class of the original variable is preserved after unpacking. I had hoped that the new matlab command extractfield would do this, but it doesn't, since, it appears, any nonnumeric variable is output as a cell array. Is there a clean way of doing this? Here's an example
myCell = {'word'};myString = 'word';S.cellVar = myCell;S.stringVar = myString;FieldNames = fieldnames(S);for ii=1:numel(FieldNames); eval([ FieldNames{ii} '=extractfield(S,FieldNames{ii});']);end;class(cellVar)class(stringVar)
I would like a clean way of ensuring that character strings like stringVar are output as strings not cells. Or perhaps somebody has a better way altogether of passing multiple variables to subroutines.
Thanks for any recommendations.
Best Answer