Variably assigning names to graphs created in a loop

7 次查看(过去 30 天)
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
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

类别

Help CenterFile Exchange 中查找有关 Labels and Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by