Variably assigning names to graphs created in a loop

6 views (last 30 days)
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

Accepted Answer

the cyclist
the cyclist on 6 May 2015
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

More Answers (2)

Guillaume
Guillaume on 6 May 2015
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 on 6 May 2015
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!