Variably assigning names to graphs created in a loop

7 ビュー (過去 30 日間)
Carolin Brueckmann
Carolin Brueckmann 2015 年 5 月 6 日
コメント済み: Guillaume 2015 年 5 月 6 日
Hi there,
I am trying to produce 19 plots for different time series and have Matlab assign them names according to an index. With my code I achieve to name the 19 graphs according to the number i (so 1,2,..,19), but I would like to have the names of the index given to the current i (so if i is 3 in the current run the name of the graph should be HYB).
Index = 1; % 1 = BarclaysUSCreditBondIndex, 2 = Hjrentelande, 3 = HYB, 4 = Ink1Y, 5 = Ink3Y, 6 = KFX, 7 = MSCIEmergingMarkets, 8 = MSCIEuropa, 9 = MSCIFjernstenexJapan, 10 = MSCIJapan, 11 = MSCIUSA, 12 = Real3Y, 13 = Real5Y, 14 = Real7Y, 15 = Stat2Y, 16 = Stat3Y, 17 = Stat5Y, 18 = Stat7Y, 19 = Virkobl
nIndices=3
for i=1:nIndices
figure
plot(Date(2:end), returns(:,i))
datetick('x')
xlabel('Date')
ylabel('Return')
title(sprintf(' %d' ,i))
end
I would appreciate any Tips!
Thanks in advance.
Carolin

採用された回答

the cyclist
the cyclist 2015 年 5 月 6 日
Here is how I usually do that:
IndexList = {'BarclaysUSCreditBondIndex','Hjrentelande','HYB'};
nIndices = numel(IndexList)
for i = nIndices
figure
plot(rand(5))
title(['This is the plot for index ',IndexList{i}])
end

その他の回答 (2 件)

Guillaume
Guillaume 2015 年 5 月 6 日
Simply use a cell array for the graph names:
graphnames = {'BarclaysUSCreditBondIndex', 'Hjrentelande', 'HYB', 'Ink1Y', 'Ink3Y', ...
'KFX', 'MSCIEmergingMarkets', 'MSCIEuropa', 'MSCIFjernstenexJapan', ...
'MSCIJapan', 'MSCIUSA', 'Real3Y', 'Real5Y', 'Real7Y', 'Stat2Y', ...
'Stat3Y', 'Stat5Y', 'Stat7Y', 'Virkobl'};
for graph = 1:nindices
%... plot graph
title(graphnames{graph});
end

Carolin Brueckmann
Carolin Brueckmann 2015 年 5 月 6 日
Hi,
thanks to both of you! I didn't get @ Guillaume 's code to run, but @the cyclist 's addition works perfectly.
I really appreciate it!
Have a good day.
Carolin
  1 件のコメント
Guillaume
Guillaume 2015 年 5 月 6 日
Hum! Apart from different variable names, both codes are identical.

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

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by