Dynamically name a struct
조회 수: 36 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Guillaume
2015년 12월 14일
편집: Guillaume
2015년 12월 14일
While it is possible to generate variables dynamically (just use eval) it's not recommended practice. Generating the variable name from user input, particularly, is asking for trouble. The way variables work in matlab, they shadow functions of the same name. So if the user decides to name your variable plus, that's addition broken in the rest of your program, name it eq and it's comparison that's broken.
You also have no guarantee that the string entered by the user is a valid variable name. So you have to pass the user input through genvarname or matlab.lang.makeValidName, but then the actual variable name may be different from the user input. Possible confusion...
I'm not clear on your end goal, but if what you want is a mapping between an arbitrary (user input) string and a value then you could use a map. Matlab's implementation of maps is a bit clunky but it does work.
추가 답변 (2개)
Walter Roberson
2015년 12월 14일
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
Image Analyst
2015년 12월 14일
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.
Image Analyst
2015년 12월 14일
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
Image Analyst
2015년 12월 14일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!