How to rename an existing structure A by assigning it a name stored in a string

98 Ansichten (letzte 30 Tage)
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 Kommentare
Bruno Luong
Bruno Luong am 15 Aug. 2020
Strong recommendation: Don't do it.
You'll be stuck to make any program downstream with variable name defined by a string.
Stephen23
Stephen23 am 15 Aug. 2020
Bearbeitet: Stephen23 am 15 Aug. 2020
" 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).

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Mehmed Saad
Mehmed Saad am 15 Aug. 2020
You can use assignin for that purpose
A = struct('X',1,'Y',2)
new_name='B';
assignin('base',new_name,A);
disp(B)
B =
struct with fields:
X: 1
Y: 2

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by