How to rename an existing structure A by assigning it a name stored in a string
88 次查看(过去 30 天)
显示 更早的评论
Sorry, this is very basic, but I could not get the answer from previous posts:
How to rename an existing structure A by assigning it a name new_name stored in a string name_string?
A=struct
new_name=”name_string”
How can I rename A to name_string?
2 个评论
Bruno Luong
2020-8-15
Strong recommendation: Don't do it.
You'll be stuck to make any program downstream with variable name defined by a string.
Stephen23
2020-8-15
编辑:Stephen23
2020-8-15
" this is very basic..."
and the answer is also very basic: dynamically naming variables is one way that beginners force themselves into writing slow, complex, obfuscated, buggy, hard-to-debug code:
"... but I could not get the answer from previous posts"
I am very surprised that you could not find any information on this, expecially considering how this topic has been discussed thousands of times on this forum (I have probably read most of the related threads).
采纳的回答
Mehmed Saad
2020-8-15
A = struct('X',1,'Y',2)
new_name='B';
assignin('base',new_name,A);
disp(B)
B =
struct with fields:
X: 1
Y: 2
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!