MATLAB: Preserving variable class when extracting fields from a struct

extractfieldglobal

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

All of that "packing" and "unpacking" is a total waste of time, space and code. Just define those values to be part of the structure right from their definition, pass the structure as an input argument to that function, and then use the fields of the structure directly within that function.
And of course avoid using global, or creating dynamically named variables in MATLAB. This is poor practice as has been explained many times on this forum, and is not recommended by MATLAB themselves:
When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.
However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.
In your case a much more robust solution would be to use:
There are many functions that support working on structures and cell arrays and can access their data easily, and many functions operate on complete numeric arrays all at once without any loops (i.e. vectorized code, which is something you need to learn about).
Placing your data in a structure or cell array also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?
If you have a newer version of matlab you can also use a table , which stores the data together in one array but also allows key-name access to the columns. This might be a good alternative for your data.
In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:
Here is a discussion of why it is a bad idea to include meta-data (such as an index) in a variable name:
And here is a discussion of why it is a really bad idea to make variables magically appear any workspace (even though beginners love doing this):
And in case you thought this is a MATLAB restriction, here is the same discussion for some other languages, advising "DO NOT create dynamic variable names":