How can I correct this error?
    조회 수: 2 (최근 30일)
  
       이전 댓글 표시
    
Hi, so as I run my code and it works but gets an error.
figure(1)
 for k = 1:24:nfiles
     nexttile
     vend = 1:24;
     bar(All_Horas(vend+k-1),All_PpH(vend+k-1))
     xlabel('Hores'),ylabel('Distancia (km)');
     title(sprintf('Març %d',floor(k/24)+1))
 end
the error is the followed:
 Index exceeds the number of array elements. Index must not exceed 496.
I understand why it happens,  k goes in packs of 24 and there are just 496 elements so the division is not an entire number, but how can I make that the last plot has just the last elements till it reaches 496? Thank you in advance
댓글 수: 0
채택된 답변
  Voss
      
      
 2022년 2월 22일
        % creating some random data:
nfiles = 496;
All_Horas = 1:nfiles;
All_PpH = randn(1,nfiles);
figure(1)
vend = 1:24;
for k = 1:24:nfiles
    nexttile
    idx = vend+k-1;
    idx(idx > nfiles) = []; % remove any indexes greater than nfiles
    bar(All_Horas(idx),All_PpH(idx))
    xlabel('Hores'),ylabel('Distancia (km)');
    title(sprintf('Març %d',floor(k/24)+1))
end
% now the last plot has 16 bars
댓글 수: 0
추가 답변 (1개)
  David Hill
      
      
 2022년 2월 22일
        
      편집: David Hill
      
      
 2022년 2월 22일
  
      figure(1)
 for k = 1:24:480
     nexttile
     vend = 1:24;
     bar(All_Horas(vend+k-1),All_PpH(vend+k-1))
     xlabel('Hores'),ylabel('Distancia (km)');
     title(sprintf('Març %d',floor(k/24)+1))
 end
 bar(All_Horas(481:496),All_PpH(481:496))
 xlabel('Hores'),ylabel('Distancia (km)');
 title(sprintf('Març %d',20));
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



