MATLAB Answers

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

3 ビュー (過去 30 日間)
Damian Maxwell
Damian Maxwell 2020 年 8 月 15 日
編集済み: Stephen 2020 年 8 月 15 日
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 件のコメント
Stephen
Stephen 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
Mehmed Saad 2020 年 8 月 15 日
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
  1 件のコメント
Damian Maxwell
Damian Maxwell 2020 年 8 月 15 日
Thank you so much Mehmed! Works! :)

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by