Extracting fields from structures with varying names.
12 次查看(过去 30 天)
显示 更早的评论
Hi I have several structures on my workspace. Their names are unrelated (e.g. "data_23_200_600_451", "data_58_588_154_289" etc.), but they all have identical field names. The tricky names of the structures make it difficult to extract the the same field in all structures using a loop (i.e. the names are not 'data_1', 'data_2' etc.) Is there any other method to automatically extract the fields from these structures? Thank you!
4 个评论
Guillaume
2016-4-13
The question is then: how do you want to identify which variables you want to iterate over? Are you planning to provide them as a list of variable names? Or should they be identified automatically because they are structures with the correct fields? Or ...?
Stephen23
2016-4-13
编辑:Stephen23
2016-4-14
"The tricky names of the structures make it difficult to extract the the same field in all structures using a loop"
Yes it will be tricky, because the bad program design makes it tricky. Bad program design makes for buggy, inefficient programs. And putting meta data into variable names is one of the slowest and buggiest ideas that beginners keep dreaming up. Any "solution" to "loop over" those variables is going to be a slow, pointless, buggy workaround for a poor design decision:
"If the structures were created without resorting to metadata names it would be difficult to keep a track on them" It is much easier to put the meta data in fields of the structure: faster, easier, and trivial to access.
采纳的回答
Fangjun Jiang
2016-4-12
Use this example to see if it helps to resolve your issue
clear;
data_23_200_600_451=struct('strings',{{'hello','yes'}},'lengths',[5 3]);
data_58_588_154_289=struct('strings',{{'abc','efg'}},'lengths',[5 3]);
save;
Vars=load;
StructNames=fieldnames(Vars);
for k=1:numel(StructNames)
Vars.(StructNames{k}).strings
end
2 个评论
Guillaume
2016-4-13
If this solve your problem, it's the most convoluted and slowest way of solving it. Saving the workspace to a file and reading it again is a complete waste of time.
You haven't answered my latest comment, but if this work, does this mean that all the variables in the workspace are structures you want to iterate over?
更多回答(2 个)
Guillaume
2016-4-13
As per my comment to the accepted answer, save and load is the most inefficient way of solving your problem (and could fail if you don't have write access to the current directory). If all the variables in the workspace are all structures you want to access, then using who is the fastest way:
"If the structures were created without resorting to metadata names it would be difficult to keep a track on them". No, there are many ways of keeping track of variables without using eval and without embedding metadata in the variable name: tables, structures, maps, etc. It would have been better for you to ask how to do that first rather than asking how to solve the mess you've created.
Anyway, this will be faster than save and load and will clean up the mess a bit:
varnames = who; %query the name of all workspace variables
%now clean up a bit by putting them all in a structure:
data = struct();
for varname = varnames'
data.(varname{1}) = eval(varname);
clear(varname);
end
%all your orignal structures are now fields of data.
%to iterate over them:
for varname = fieldnames(data)'
x = data.(varname{1}).somecommonfield;
end
3 个评论
Guillaume
2016-4-14
编辑:Guillaume
2016-4-14
Writing data to disk to just read it again is going to orders of magnitude slower than reading it straight from memory.
If the data is already save in a .mat file, then we're going back to my initial comment to the question. It's better not to create those variables in the first place, and indeed load with a return argument is one way to solve this. However, the OP has indicated that these variables were created with eval.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!