Code only works in the command window

9 次查看(过去 30 天)
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 个评论
OCDER
OCDER 2019-2-8
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.
Guilherme Lopes de Campos
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

请先登录,再进行评论。

采纳的回答

OCDER
OCDER 2019-2-8
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 个评论
Jan
Jan 2019-2-12
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.
Guilherme Lopes de Campos
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Conway's Game of Life 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by