Dynamically name a struct
Show older comments
I'm generating a GUI which users will type the name of a test and then generate structs based off of the test type. Right now they have 4 selections to choose from for struct naming (Body, Head, Leg, and New). I need this ability for the "New" element so that this code doesn't become useless when new test types are generated. (I'm only stating this because there are plenty of answers with naming struct fields that argue that I should never need to do this).
I have a temporary variable k, that holds the struct until the end, where I would like to essentially go (variable) = k, and have it generate
k.TestName k.SignalName k.Signals.. etc
(variable) = k;
(variable).TestName (variable).SignalName (variable).Signals.. etc
Any and all ideas would be greatly appreciated.
Best, ML
Accepted Answer
More Answers (2)
Walter Roberson
on 14 Dec 2015
For your application of combining .mat files, you should use
structname = load(TheFile);
and combine the structs into other structs, and when the user inputs the name of a variable, pull it out of the combined struct using dynamic field names. When it comes time to write to a .mat file, use save() with the -struct flag.
4 Comments
Mary
on 14 Dec 2015
Walter Roberson
on 14 Dec 2015
I am saying you should rewrite your code so that it does not ever refer to a user-named variable, and instead refers to a user-named field within a struct.
combined_struct = struct();
known_fields = sort(fieldnames(combined_struct));
...
this_file_struct = load('UserSelectedMatFile.mat');
these_fields = fieldnames(this_file_struct);
num_fields = length(these_fields);
for K = 1 : num_fields
this_field = these_fields{K};
combined_struct.(this_field) = this_file_struct.(this_field);
end
known_fields = sort(fieldnames(combined_struct));
set(handles.known_fields_popup, 'String', known_fields); %update user list
...
new_fieldname_user_chose = 'SuperSeiyan';
combined_struct.(new_fieldname_user_chose) = []; %create it
....
save('OutputFileName', 'combined_struct', '-struct');
The -struct flag instructs MATLAB to not save combined_struct as a variable and to instead create one variable in the .mat file for each field in combined_struct, pulling apart the structure into individual variables in the .mat file.
Image Analyst
on 14 Dec 2015
Like all 3 of us said, it's not a good idea to let your variable names be named according to what string your users type in.
Make that four people giving the advice to avoid dynamically creating variables like that. It is a Really Bad Way To Code.
Image Analyst
on 14 Dec 2015
You know what they selected, right? Like if they typed it in or chose a radio button or picked it from a listbox or popup? So just assign the name based on what they chose. Let's say you have boolean variables head, body, etc. that say whether that body part was chosen or not. Then just do
if bodySelected
body = k;
elseif headSelected
head = k;
elseif legSelected
leg = k;
end
3 Comments
Image Analyst
on 14 Dec 2015
Why do you let your users decide what your variable name in your program should be called? That does not seem like a wise idea.
Mary
on 14 Dec 2015
Categories
Find more on Structures 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!