Hello
I have been trying to plot some evolution of parameters as a function of years. Here are my trials with codes. Figure 0 is the default which starts from zero to Nyears. Figure 1 is developed with new vector xdate with each element is integer value of each year in four digits. But I wanted in two digits. After I created the list of strings with two digits, the labels are repeated as in Figure 2. Could you please help me how to fix such issues in Matlab.
X = [0.4851 0.12477 0.079046 0.56795 7.95E-05 0.49443 0.0722 0.87595 ... 0.5065 0.41204 0.69801 0.70715 0.14759 0.54556 0.60394 0.46758 ... 0.30115 0.48743 0.35104 0.0059046 0.21027 0.077238 0.66774 0.28482 ... 0.081285 0.3046 0.32423 0.19326 0 0.34675 0.044938 0.34572 ... 0.68653 0.88543 0.51209 0.15263 0.28867 0.63446 0.027579 0.010771 ... 0.67618 0.75188 0.83475 0.34111 0.43898 0.5252 0.38063 0.50016 ... 0.54488 0.41151 0.31144];Nyears = 52;yearStart = 1956;yearEnd = Nyears + yearStart;xdate = yearStart+1:yearEnd;istart = 2;nend = Nyears;xdate1 = xdate(istart:nend);% calls a routine to create a vectors of strings for each years
[ idyears ] = stringYearLabel( yearStart, Nyears ); xtickLbl = [idyears(istart+1); idyears(5:10:end); idyears(end)];plot(X, 'color', 'k'); %default plot as in Figure 0
plot(xdate1, X, 'color', 'k'); %include time as vector for X values
%set the limit of X axis; by default figure looks like Figure 1
set(gca, 'Xlim', [xdate1(1) xdate(end)]);
% while setting like this plot come out to be terrible (Figure 2), labels limited
set(gca, 'Xticklabel', xtickLbl);
function [ id2digit, idyears ] = stringYearLabel( startYear, Nyears )% to generate vectors of year labels in string
% id2digit : lists in two digits
% idyears vectors in four digits
idyears = cell(Nyears+1,1);id2digit = cell(Nyears+1,1);k = 0;for i = 1:Nyears+1 idyears{i} = int2str(k + startYear); id2digit{i} = idyears{i}(end-1:end); k = k + 1; end
Thank you for your help…
Best Answer