different titles for animation

5 views (last 30 days)
Nahid Atashi
Nahid Atashi on 23 Jul 2019
Commented: Nahid Atashi on 23 Jul 2019
Hi friends,
I have a code to make an animation which is fine and works but i need to creat title for each map
in this animation. I tried but couldn't make it as a loop.
Maps are monthly for 40 years from 1979:2018. the titles should be like: {'Jan 1979', 'Feb 1979',.................'Dec 1979', 'Jan 1980'.........}
and so on (12 month for each year).
could some body help me please?
thanks in advance
This is the main part of the code:
%% creat animat
for i=1:12
a=reshape(monthly(i,:),81,65);
a(~inMap)=nan;
for k=1:12
[c,h]=contourf(XX,YY,a ,'LineStyle','none');colorbar;
hold on
plot(lonos(:,1),latos(:,1),'black');
title (sprintf('% JAN 1979',k));
pause(1)
% input('-->','s');
end
end

Accepted Answer

David K.
David K. on 23 Jul 2019
I would suggest creating the title like this:
months = {'Jan', 'Feb', ...};
years = num2str((1979:2018)');
for i = 1:40
for k = 1:12
title(strcat(months{k}," ",years(i,:)));
end
strcat is a string concatenation -> It puts the strings together. I assumed the outer loop is meant to be years so I changed it from 12 to 40 since you mentioned 40 years.
-Also note, it is highly recommended that you do not use i as a variable. It is the built in constant for the imaginary number.
  1 Comment
Nahid Atashi
Nahid Atashi on 23 Jul 2019
thanks so much David for your great answer. it works perfectly.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!