Is it possible to extract all fields from a structure automatically?
Show older comments
I have data in the form: mydata.x = 100; mydata.s = 'abc'; mydata.Y = [1 2 3]; And I want variables x = 100; s = 'abc'; Y = [1 2 3]; How to extract variables automatically? (suppose that I don't know the names of the variables!)
7 Comments
What you want is to magically create new variables in your workspace without knowing what they are when you write the code. This is a really bad idea (slow, buggy, impossible to debug, etc), which had been explained to you before, in lots of your own questions:
Do you imagine that something has changed since you asked those questions? Why beginners love to use a programming method that removes so many useful tools and code checking is beyond my understanding.
The programming best practice would be to simply to use the structure. What is wrong with doing that ?
Mr M.
on 10 Aug 2016
"Is it possible"
Of course it is possible. Clearly you didn't bother to read any of those links in your own questions, because they all explain how (or have links to an explanation). They also explain why it is very poor programming.
- beginner: How can I magically create variables?
- expert: don't do that, it is slow and buggy. Here are some better ways...
- beginner: No! I am better than all of those professional programmers who have written advice to avoid doing this! Even those so-called experts at MATLAB who write that magically creating variables is a bad idea, they are just stupid. I am much more cunning than them!
- expert: here are some much more reliable and easy ways to...
- beginner: Haha!, I just created the variables magically!
- expert: sigh.
One week later:
beginner: why is my code so slow? and I cannot fix these strange bugs...
Image Analyst
on 10 Aug 2016
Why does your structure have hundreds of variables in the first place? You should probably use an array or a table instead of a structure if you're going to need that many fields.
Mr M.
on 10 Aug 2016
"Why anybody think that this is a dynamic variable in my example?"
Because that is exactly what load does, when called without an output argument: it dynamically creates all of the loaded variables in the caller workspace:
https://www.mathworks.com/matlabcentral/answers/335253-mat-files-not-loading-correctly#answer_262994
And of course:
Accepted Answer
More Answers (3)
Alternative, maybe this is more desired than dictating how the struct will be saved in the workspace (this will recursively unpack any struct within the struct):
function unpackStruct (structure)
fn = fieldnames(structure);
for i = 1:numel(fn)
fni = string(fn(i));
field = structure.(fni);
if (isstruct(field))
unpackStruct(field);
continue;
end
assignin('base', fni, field);
end
end
6 Comments
Steven Lord
on 14 Nov 2019
This has all the same problems as calling eval plus it creates the variables in the base workspace. This has many problems, among them a cluttered base workspace (making it difficult to find a particular needle in a huge haystack) and storing the data where any other function that runs can manipulate it, meaning you can't tell (without checking every single time) whether the data on which you're operating is the same as the data from your struct.
Baium
on 14 Nov 2019
Makes sense. Also, I realized that I am not sure what will happen if a nested struct has the same field name with an existing variable in the workspace - I guess it will overwrite it. It's got flaws, agreed.
"It's got flaws..."
Slow, complex, obfuscated, liable to bugs (e.g. overwrites variables without warning, as you note), difficult to debug... Nothing new, the same as all other methods of dynamic variable accessing:
"I realized that I am not sure what will happen if a nested struct has the same field name with an existing variable in the workspace"
In fact this approach lets the data overwrite any variable in the code, not just the imported ones: data arrays, filenames, constant parameters, etc. can be overwritten without warning.
Kent Schonert
on 16 Jul 2020
This method can be useful, but I recommend replacing assignin('base',... with assignin('caller',...
Alec Jacobson
on 9 May 2021
Or as a single anonymous function (which you can drop into your ide)
unpackStruct = @(s) cellfun(@(name) assignin('base',name,getfield(s,name)),fieldnames(s));
Ludmila Kuncheva
on 24 Aug 2021
Thank you, Alec! Very useful.
Azzi Abdelmalek
on 10 Aug 2016
Edited: Azzi Abdelmalek
on 10 Aug 2016
Use fieldnames function
mydata.x = 100;
mydata.s = 'abc';
mydata.Y = [1 2 3];
field=fieldnames(mydata)
Then you want to assign to each variable individually a corresponding value, which is not recommended. Read this http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
who / whos function when called without any argument, lists all the variables in the current workspace with their properties.
Categories
Find more on Data Type Identification 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!