Ploting dates on x-axis in octave: “error: __plt2vv__: vector lengths must match”

我只是一个虾纸丫 提交于 2019-12-11 16:15:53

问题


I'm trying to plot 3 lines on the same figure with dates on X-axis . I can do it perfectly on without having dates on x-axis but when I try to put years on x-axis I get this error :

    error: __plt2vv__: vector lengths must match

I'm not familiar with MATLAB and this is my first tiral , here is the part of my code where I try to plot :

        data = importdata('2.txt');
        C = data.data.';
        C = C';

        N = length(C);
        H = 12;

        w = ones(2 * H + 1, 1);


       Lambda_Tilde = NaN * zeros(N, 1);
       L_Tilde = NaN * zeros(N, 1);
       U_Tilde = NaN * zeros(N, 1);
       for t = (H+1):(N-H-1)
          Lambda_Tilde(t) = sum(w .* C(t-H:t+H)) / sum(w);
          L_Tilde(t) = poissinv(0.005, Lambda_Tilde(t));
          U_Tilde(t) = poissinv(0.995, Lambda_Tilde(t));
       end



      clf
      f=figure();
      t= (1996 : 2007);
      dateFormat = 10;
      datetick('x',dateFormat)
      plot(datenum(t,1,1), C, 'co*');
      hold on
      plot(datenum(t,1,1) , L_Tilde, 'g-.');
      plot(datenum(t,1,1) , U_Tilde, 'g-.');
      pause;

Now I understood where the problem is . C is a 1*144 Matrix, so is there a way to try to plot

     plot(1:N , C , 'co*' ) 

and having dates on xaxis instead of having random numbers? I will appreciate any guidance .


回答1:


I think what you want to do is change the shape of t so that it is a 1x144 matrix with repeated dates in it (1996 12x, 1997 12x etc). Then you can plot(t,C) and it should work. To change the shape, you can use:

dates = [1996:2007];  %Creates the original dates matrix (your t)
N = size(dates,2);    %finds the length of that matrix (12 in your case)
M = 12;               %sets the number of repetitions
dates= repmat(dates,M,1);     %repeats each date M times, but they are still listed in columns
dates = reshape(dates,N*M,1); %changes the shape from columns containing the same shape to 1 column


来源:https://stackoverflow.com/questions/26240126/ploting-dates-on-x-axis-in-octave-error-plt2vv-vector-lengths-must-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!