Hi:
I have various variables in the workspace: january_ns=1, february_ns=2, march_ns=3,.... Also I have an array like this: month={january february march }
I would like to show the content of the variable january_ns for example but using the array to show it.
To write month{1}_ns instead january_ns to show the value 1, for example.
I was trying with the next line, but it didnt work.
([month{1},'_ns',''])
Any idea to do that? Thanks.

 採用された回答

Anthony
Anthony 2013 年 10 月 18 日

0 投票

january_ns=1, february_ns=2, march_ns=3
month={'january' 'february' 'march' };
eval([month{1},'_ns'])

2 件のコメント

jose bernardo
jose bernardo 2013 年 10 月 18 日
Thank you Anthony, It works perfectly.
Saludos!
Jan
Jan 2013 年 10 月 18 日
@jose: It might work now, but Azzi's suggestion shows a much better strategy.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 18 日
編集済み: Azzi Abdelmalek 2013 年 10 月 18 日

2 投票

month={'january' 'february' 'march' }
month{1}
month{2}
% You don't need to create a variable january_ns. Can you explain why you want to create these variables?

4 件のコメント

jose bernardo
jose bernardo 2013 年 10 月 18 日
Thank you Azzi.
The variables with the sufix 'ns' were all in the workspace, and I needed to do a loop with them, but not all, only the variables that appeared in the array (imported from excel).
But with the command "eval" do all ok.
Un saludo.
Jan
Jan 2013 年 10 月 18 日
+1: This solution is much better than the eval approach. It is a bad programming practice to store important information in the name of variables, because carrying values is a job for the contents of a variable, not for the name!
Anthony
Anthony 2013 年 10 月 19 日
編集済み: Anthony 2013 年 10 月 19 日
no doubt that it is not advisable to use 'eval' , well, in most cases. (just give what is closest to the code you describe without knowing what/why you want to do. not intend to teach any coding habit :P)

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2013 年 10 月 18 日

コメント済み:

2013 年 10 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by