Extracting everything from a structure

Is there a built in function to extract everything from a structure?
For example if I have a structure:
struct_name.struct_element1 = 7;
struct_name.struct_element2 = 'abc';
Then later I want create variables for every element of struct_name but I'm unaware of how many elements are in "struct_name" and what their names are. I wrote the following code to solve my problem but I feel there might be a more elegant solution or a built in function:
struct_field_names = fieldnames(struct_name);
for k = 1:length(struct_field_names)
eval([struct_field_names{k}, ' =...
struct_name.',struct_field_names{k}, ';']);
end

更多回答(3 个)

Take this into account:
struct_name.struct_name = 1;
struct_name.data2 = 2;
Now the result of the creation of variables depends on the order of fields - a dangerous programming method.
So follow Oleg's link and avoid the dynamic creation of variables. Using ISFIELD and "dynamic fieldnames" is a more reliable technique.
Think if you had 20 fields in the struct, you would end up populating the workspace with 20 different variables which you will have eiter use by manual referencing or by using eval.
Both methods are nightmare, but you're welcome to abuse eval for some time. The problem comes later when you have to mantain your code.

4 个评论

Thanks for your response. I do have about 20 variables that are used extensively in math and logical calculations. I fear passing all of those variables individually to the function is dangerous because it's too easy to mix up the sequence of inputs. So I would like to pack everything into a struct and pass that to the function then just unpack it all inside the function. I prefer to unpack it, rather than reference it from the structure because y = m*x + b looks a lot neater than z.y = z.m*z.x + z.b or whatever.
You don't know ahead of time whether any particular element will be there or not, but you are going to go ahead and blindly use it by name?? Or what?
Is neat code more important than correct code? Proceeding the way you are will likely produce wrong code. What happens in your routine when you decide to add a field named 'k' to the structure?
Good points. I can create a list of variables needed rather than relying on fieldnames and add error handling for needed variables that don't exist. The function suggested on the link I posted below uses "assignin" within a for loop in a function so I believe that eliminates the concern for the assignment creating the iterator variable.
The problem isn't in creating the iterator variable: the problem would be that on the next iteration of the loop, the variable you _thought_ you copied from the structure would get overwritten.

请先登录,再进行评论。

doc struct2cell
?

2 个评论

That does well to get the values but it loses the names of the elements :-(
The values and their indeces should be all you need, per the FAQ link.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Structures 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by