Using Variable Contents to Create Variable names

30 ビュー (過去 30 日間)
Brian
Brian 2012 年 2 月 27 日
コメント済み: Stephen23 2017 年 7 月 7 日
I have two variables (RtnType and MyPG) each with their values down the rows. I would like to write the results of my code loop as I iterate through these variables to the proper variable derived by combining the values of these two variables.
RtnType = {'Daily' 'RollWeek' 'RollMonth' 'RollQtr' 'YTD' 'RollYear' 'SinceIncept'};
MyPG = {'MTAC' 'MTAM' 'MTAG' 'MLCE'};
for i = 1:4
for x = 1 to 7
MyMedian = [1.5];
%Desired Concatenation look
%MyPG{i,1} & '_' & RtnType{x,1} = [MyPG{i,1} & '_' & RtnType{x,1},[DailyMedian]
%Actual statement result for i=1 and x=1
MTAC_Daily = [MTAC_Daily; MyMedian]
end
end
Can someone show me how to write the results of each loop to different vectors that have been pre-created?
Thanks a lot, Brian
  1 件のコメント
Stephen23
Stephen23 2017 年 7 月 7 日
Do NOT waste your time writing bad code that creates separate variables with numbered variable names: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended."
Just use indexing: simple, fast, efficient, neat, simple, easy to understand, much less buggy, easier to debug, etc, etc, etc.
etc, etc, etc.

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

採用された回答

Jonathan Sullivan
Jonathan Sullivan 2012 年 2 月 27 日
eval([MyPG{i} '_' RtnType{x} '= [' MyPG{i} '_' RtnType{x} ',' num2str(MyMedian) '];'])
  1 件のコメント
Brian
Brian 2012 年 2 月 27 日
Thanks this answer works perfectly. The only thing I added was decimal precision on the num2str command to prevent it from rounding me to 4 decimal places.
Thanks a lot!

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

その他の回答 (1 件)

Honglei Chen
Honglei Chen 2012 年 2 月 27 日

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by