Acquiring variable names to be used in fieldnames

2 vues (au cours des 30 derniers jours)
Abhi Ove
Abhi Ove le 27 Nov 2016
Commenté : Stephen23 le 6 Déc 2016
Hi, I have a structure (abcSTRUCTUREdef) in my work space containing a list of variables. I use the following function to get all the varibles from the structure as a workspace variables.
%%%%%%%%%%Original Code %%%%%%%%%%%
fields = fieldnames( abcSTRUCTUREdef, '-full'); % Find out how many there are - for our loop.
numberOfFields = length(fields);
for f = 1 : numberOfFields;
thisField = fields{f};
commandLine = sprintf('%s = abcFIELDdef.%s', thisField, thisField);
eval(commandLine);
end
% Release temporary variables.
clear('f', 'thisField', 'numberOfFields');
clear('fields', 'commandLine');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
However I would like to automate the whole process by just using it as a script where it acquires the structure name from the work space such that, (for example)
varNames = who;
structName = varNames{1,1};
fields = fieldnames( structName, '-full'); % Find out how many there are - for our loop.
commandLine = sprintf('%s =' structName '.%s', thisField, thisField);
However everytime I attempt this, I get an error saying that "Undefined function 'fieldnames' for input arguments of type 'char'."
This is the end of a long script that pulls data out of folders. I would really appreciate your help.
Many thanks.
  3 commentaires
Abhi Ove
Abhi Ove le 28 Nov 2016
Modifié(e) : Abhi Ove le 28 Nov 2016
Hi I have multiple structure in my workspace. I would like to:
1) workSpaceVarName = who; % to acquire a list of variables in my workspace and then specifically use the field function
2) NAMES = fieldnames( workSpaceVarName{1,1} ,'-full') % to get all the details of each structure.
I hope I make sense, but please do let me know if I do not.
Many thanks.
Stephen23
Stephen23 le 6 Déc 2016
@Abhi Ove: thank you for your explanation. What you are trying to do is not a good way to write code. Please read my comment and its links to know why. The links also explain better ways to write code.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 27 Nov 2016
"NAMES = fieldnames(S) returns a cell array of character vectors containing the names of the fields in structure S.
NAMES = fieldnames(Obj,'-full') returns a cell array of character vectors containing the name, type, attributes, and inheritance of the properties of Obj. Only supported for COM and Java objects."
You are trying to apply fieldnames to a cell array of strings that are variable names. There is no direct MATLAB operation to get the fieldnames of a variable whose name you know.
  1 commentaire
Walter Roberson
Walter Roberson le 28 Nov 2016
There is no direct way to ask for the fieldnames of a variable whose name is known. You would need
NAMES = fieldnames( eval(workSpaceVarName{1,1}) );

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by