Code only works in the command window

Hi MATLAB community,
I am trying to execute the follow code, but this only works in the command window and not editor.
Could you help me?
Guilherme Lopes de Campos
% dados is a matriz 13817 rows and 3 columns
function [matriz_medias] = gerar_medias(dados)
[linha,coluna]=size(dados);
matriz(1:linha,1:4)=zeros;
matriz(1:linha,2:4)=dados;
j=1;
k=1;
ano_ini=matriz(1,3);
for i=k:linha
if matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
while matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
end
while matriz(k,3)>ano_ini
matriz(k,1)=j;
k=k+1;
end
end
j=j+1;
end
matriz_media=zeros(((matriz(linha,3)-matriz(1,3))+1)*12,(matriz(linha,1)+2));
instrumento=1;
ano=matriz(1,3);
mes=1;
for i=1:((matriz(linha,3)-matriz(1,3))+1)*12
if rem(i,12)~=0
matriz_media(i,1)=ano;
matriz_media(i,2)=mes;
mes=mes+1;
else
matriz_media(i,1)=ano;
matriz_media(i,2)=mes;
mes=1;
ano=ano+1;
end
end
mes=1;
n=0;
j=1;
soma=0;
instrumento=1;
diferenca=0;
for i=1:linha
if matriz(i,2)==mes
soma=soma+matriz(i,4);
n=n+1;
else
matriz_media(j,instrumento+2)=soma/n;
soma=0;
n=0;
diferenca=matriz(i,2)-mes;
if diferenca>1
for k=1:(diferenca-1)
j=j+1;
matriz_media(j,instrumento+2)=999999;
mes=mes+1;
end
diferenca=0;
end
soma=matriz(i,4);
n=1;
if mes<12
mes=mes+1;
else
mes=1;
end
if matriz(i,1)~=instrumento
instrumento=instrumento+1;
j=j+1;
end
end
end
end

2 Comments

How are you executing your code in the command window and what do you mean it runs in the editor? Are you getting an error message? If so, copy paste the full error message.
Hi OCDER, thank you very much your help,
The code:
dados=table2array(D);
[matriz_medias]=gerar_medias(dados);
function [matriz_medias] = gerar_medias(dados)
[linha,coluna]=size(dados);
matriz(1:linha,1:4)=zeros;
matriz(1:linha,2:4)=dados;
j=1;
k=1;
ano_ini=matriz(1,3);
for i=k:linha
if matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
while matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
end
while matriz(k,3)>ano_ini
matriz(k,1)=j;
k=k+1;
end
end
j=j+1;
end
matriz_media=zeros(((matriz(linha,3)-matriz(1,3))+1)*12,(matriz(linha,1)+2));
instrumento=1;
ano=matriz(1,3);
mes=1;
for i=1:((matriz(linha,3)-matriz(1,3))+1)*12
if rem(i,12)~=0
matriz_media(i,1)=ano;
matriz_media(i,2)=mes;
mes=mes+1;
else
matriz_media(i,1)=ano;
matriz_media(i,2)=mes;
mes=1;
ano=ano+1;
end
end
mes=1;
n=0;
j=1;
soma=0;
instrumento=1;
diferenca=0;
for i=1:linha
if matriz(i,2)==mes
soma=soma+matriz(i,4);
n=n+1;
else
matriz_media(j,instrumento+2)=soma/n;
soma=0;
n=0;
diferenca=matriz(i,2)-mes;
if diferenca>1
for k=1:(diferenca-1)
j=j+1;
matriz_media(j,instrumento+2)=999999;
mes=mes+1;
end
diferenca=0;
end
soma=matriz(i,4);
n=1;
if mes<12
mes=mes+1;
else
mes=1;
end
if matriz(i,1)~=instrumento
instrumento=instrumento+1;
j=j+1;
end
end
end
end
The script above, shows this error:
Index in position 1 exceeds array bounds (must not exceed 13817).
Error in matriz_media>gerar_medias (line 20)
while matriz(k,3)>ano_ini
Error in matriz_media (line 2)
[matriz_medias]=gerar_medias(dados);
When I have executed each row of code in the command window, the code works, but when click in Run, not.
Thank you very much

Sign in to comment.

 Accepted Answer

Looks like your while loop counter index, k, is going above the row count of matriz. For instance, check these simpler codes out to replicate your error and to fix it.
matriz = ones(1000, 3);
ano_ini = 1;
k = 1;
j = -1;
while matriz(k,3)==ano_ini %Error: Index exceeds matrix dimensions
matriz(k,1)=j;
k=k+1;
end
matriz = ones(1000, 3);
ano_ini = 1;
k = 1;
j = -1;
while matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
if k > size(matriz, 1); break; end %break while loop before an error occurs
end

5 Comments

Hi OCDER,
Now, shows the follow error:
Index in position 1 exceeds array bounds (must not exceed 324).
Error in matriz_media_depuracao>gerar_medias (line 22)
while matriz(k,3)>ano_ini
Error in matriz_media_depuracao (line 2)
gerar_medias(dados);
I modified code with your advice, such as:
gerar_medias(dados);
function [matriz_medias] = gerar_medias(dados)
[linha,coluna]=size(dados);
matriz(1:linha,1:4)=zeros;
matriz(1:linha,2:4)=dados;
matriz=ones(324,18)
ano_ini = 1;
k=1;
j=-1;
for i=k:linha
if matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
while matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
if k > size(matriz, 1); break; end %break while loop before an error occurs
end
while matriz(k,3)>ano_ini
matriz(k,1)=j;
k=k+1;
end
end
j=j+1;
end
matriz_media=zeros(((matriz(linha,3)-matriz(1,3))+1)*12,(matriz(linha,1)+2));
instrumento=1;
ano=matriz(1,3);
mes=1;
for i=1:((matriz(linha,3)-matriz(1,3))+1)*12
if rem(i,12)~=0
matriz_media(i,1)=ano;
matriz_media(i,2)=mes;
mes=mes+1;
else
matriz_media(i,1)=ano;
matriz_media(i,2)=mes;
mes=1;
ano=ano+1;
end
end
mes=1;
n=0;
j=1;
soma=0;
instrumento=1;
diferenca=0;
for i=1:linha
if matriz(i,2)==mes
soma=soma+matriz(i,4);
n=n+1;
else
matriz_media(j,instrumento+2)=soma/n;
soma=0;
n=0;
diferenca=matriz(i,2)-mes;
if diferenca>1
for k=1:(diferenca-1)
j=j+1;
matriz_media(j,instrumento+2)=999999;
mes=mes+1;
end
diferenca=0;
end
soma=matriz(i,4);
n=1;
if mes<12
mes=mes+1;
else
mes=1;
end
if matriz(i,1)~=instrumento
instrumento=instrumento+1;
j=j+1;
end
end
end
end
Everytime you use a while loop and access the kth index of a matrix, make sure there are truly k elements in a matrix. The error is the same as before, just applied to another while loop.
while matriz(k,3)>ano_ini %check your k value
Somehow, your k = 325, but matriz only has 324 rows. You cannot access the 325th row of matriz. To prevent this, make sure your while loop breaks before you try to run the iteration where 325th row is accessed. For example:
while k <= size(matriz, 1) && matriz(k, 3) > ano_ini
end
Hi OCDER,
I modified the code, but doesn't work yet, showm the follow error
Could you help me?
dados2 = table2array(d38);
gerar_medias(dados2);
function [matriz_medias] = gerar_medias(dados)
[linha,coluna]=size(dados);
matriz= ones(13817,3);
j=1;
k=1;
ano_ini=1
for i=k:linha
if matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
while matriz(k,3)==ano_ini
matriz(k,1)=j;
k=k+1;
if k > size(matriz, 1); break; end %break while loop before an error occurs
end
while k <= size(matriz, 1) && matriz(k, 3) > ano_ini
end
end
j=j+1;
end
matriz_media=zeros(((matriz(linha,3)-matriz(1,3))+1)*12,(matriz(linha,1)+2));
instrumento=1;
ano=matriz(1,3);
mes=1;
for i=1:((matriz(linha,3)-matriz(1,3))+1)*12
if rem(i,12)~=0
matriz_media(i,1)=ano;
matriz_media(i,2)=mes;
mes=mes+1;
else
matriz_media(i,1)=ano;
matriz_media(i,2)=mes;
mes=1;
ano=ano+1;
end
end
n=0;
j=1;
soma=0;
instrumento=1;
diferenca=0;
for i=1:linha
if matriz(i,2)==mes
soma=soma+matriz(i,4);
n=n+1;
else
matriz_media(j,instrumento+2)=soma/n;
soma=0;
n=0;
diferenca=matriz(i,2)-mes;
if diferenca>1
for k=1:(diferenca-1)
j=j+1;
matriz_media(j,instrumento+2)=999999;
mes=mes+1;
end
diferenca=0;
end
soma=matriz(i,4);
n=1;
if mes<12
mes=mes+1;
else
mes=1;
end
if matriz(i,1)~=instrumento
instrumento=instrumento+1;
j=j+1;
end
end
end
end
Error:
Index in position 1 exceeds array bounds (must not exceed 13817).
Error in matriz_media_depuracao>gerar_medias (line 13)
if matriz(k,3)==ano_ini
Error in matriz_media_depuracao (line 2)
gerar_medias(dados2);
Without meaningful comments it is hard to guess, what the code should do.
j = 1;
k = 1;
ano_ini = 1;
for i = k:linha
if matriz(k,3) == ano_ini
matriz(k,1) = j;
k = k+1;
while matriz(k,3) == ano_ini
matriz(k, 1) = j;
k = k + 1;
if k > size(matriz, 1);
break;
end
end
while k <= size(matriz, 1) && matriz(k, 3) > ano_ini
end
end
j = j+1;
end
The 2nd while loop is either not entered at all or runs infinetly. So you can simply omit it.
The 1st while loop fills the first column of matriz with ones, but the elements are 1 before also. I have no idea, what the prupose of this code is. I guess, that you do not need a loop at all.
Thank you very much for attention OCDER and Jan,
I didn't write the code, I will get more information and return question.
I am very grateful,
Guilherme Lopes

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!