Extracting fields from structures with varying names.
Show older comments
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 Comments
Guillaume
on 12 Apr 2016
How where these structures created in the first place?
Unfortunately, you've gone against the advice we keep on giving on this forum. Do not create variable names dynamically (with eval, assignin, load, etc.) and do not embed metadata in the variable names. Your situation is exactly one of the problem that comes from this.
While it's possible to do what you want with what you have, it's probably better to prevent the problem from occuring in the first place.
Either way, we need more details.
Karl M
on 12 Apr 2016
Guillaume
on 13 Apr 2016
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 ...?
"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.
Accepted Answer
More Answers (2)
Azzi Abdelmalek
on 12 Apr 2016
Edited: Azzi Abdelmalek
on 12 Apr 2016
0 votes
Guillaume
on 13 Apr 2016
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 Comments
Fangjun Jiang
on 13 Apr 2016
Guillaume, I don't know exactly what is the user's issue. I just sense that the difficulty is to programly access the irregular name of the structure data. Using load() with a return argument is a good way to achieve it. who() command only gives the name. You still need to use eval() to copy the data into a structure. That may not be more efficient.
Most likely, I would guess that the user already has those similar structure data saved in a .mat file. In that case, use load() is the most easy and efficient way to resolve the issue.
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.
Karl M
on 18 Apr 2016
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!