如何修正错误:“A(I):索引越界;值1超出界限0 "

时间:2021-11-09 16:40:41

So I made this function, but I don't know why I'm getting this error. How can I fix it?

我做了这个函数,但我不知道为什么会得到这个误差。我怎样才能修好它呢?

error: LOADC: A(I): index out of bounds; value 1 out of bound 0

错误:LOADC: A(I):索引越界;值1超出界限0

error: called from LOADC at line 15 column 13

错误:在第13列第15行从LOADC调用

function res=LOADC(url)
  [nomeFicheiro,sucesso]= urlwrite(url,'Composicoes.txt');
  ficheiro=fopen(nomeFicheiro,'r');
  fgetl(ficheiro);
  nLinhas=0;
  while (fgets(ficheiro) ~= -1)
    nLinhas = nLinhas+1;
  end
  for i=2:nLinhas
    linha=fgetl(ficheiro);
    pontovirgula=findstr(linha,';');
    Material=linha(1:pontovirgula(1)-1);
      for n=1:2:length(pontovirgula)
        ElemX=linha(pontovirgula(n)+1:pontovirgula(n+1)-1);
        PercentX=linha(pontovirgula(n+1)+1:pontovirgula(n+2)-1);
      end
  end
fclose(ficheiro);
res=Composicoes;
end

2 个解决方案

#1


3  

The immediate is you're trying to access a value within an empty array (which has no values in it).

当前的方法是尝试访问空数组中的值(其中没有值)。

The reason that this is happening is that you read your entire file inside of your first while loop which places the file pointer at the end of the file. Then (without resetting the file pointer), you try to read it line by line in the for loop. Since the file pointer is at the end of the file already, fgetl will always return an empty array ([]) so when you start trying to work with it, you get the indexing error that you've shown.

发生这种情况的原因是您在第一个while循环中读取整个文件,该循环将文件指针放置在文件的末尾。然后(无需重新设置文件指针),尝试在for循环中逐行读取它。由于文件指针已经在文件的末尾,所以fgetl将始终返回一个空数组([]),因此当您开始尝试使用它时,您将得到您已经显示的索引错误。

The solution is one of two options:

解决方案是两种选择之一:

  1. Call frewind(ficheiro) before the for loop to reset the file pointer to the beginning of the file so that you can successfully read each line.

    在for循环之前调用frewind(ficheiro),将文件指针重置为文件的开头,这样就可以成功地读取每一行。

  2. Come up with a better way to parse your file rather than looping through the whole file for the sole purpose of counting the number of lines in the file.

    找到一种更好的解析文件的方法,而不是为了计算文件中的行数而对整个文件进行循环。

If you post some of the file contents we can likely provide you with a better way to parse the file in one or two lines of code.

如果您发布了一些文件内容,我们可能会为您提供一种更好的方法来在一到两行代码中解析文件。

Update

更新

Also if you look at this line, n goes all the way to the end of pontovirgula.

如果你看这条线,n一直到pontovirgula的末端。

for n = 1:2:length(pontovirgula)

But then you try to access 1 and 2 past the end of the array

然后你尝试访问数组结束后的1和2

pontovirgula(n+1)
pontovirgula(n+2)

This is going to cause issues for sure. Try only looping until 2 from the end instead.

这肯定会引起问题。尝试只循环直到2结束。

for n = 1:2:(numel(pontovirgula) - 2)

Update 2

更新2

Given that you have posted the file contents, here is an alternate way to parse the file.

假设您已经发布了文件内容,这里有一种解析文件的替代方法。

fid = fopen('filename.txt', 'rt');
lines = textscan(fid, '%s', 'HeaderLines', 1);
fclose(fid);

% For each line we want to get the reference off of the front
parts = regexp(lines{1}, ';', 'split');

materials = cell(1, numel(parts));
elements = cell(1, numel(parts));
percentages = cell(1, numel(parts));

for k = 1:numel(parts)
    % Remove the last empty blank
    parts{k}(end) = [];

    % Get the material (the first element)
    materials{k} = parts{k}{1};

    % Get the elements
    elements{k} = parts{k}(2:2:end);

    % Get the percents
    percents{k} = parts{k}(3:2:end);
end

#2


0  

Check the length of linha, and the values of pontovirgula that you are using to index into linha.

检查linha的长度,以及您将其用于索引到linha的pontovirgula的值。

#1


3  

The immediate is you're trying to access a value within an empty array (which has no values in it).

当前的方法是尝试访问空数组中的值(其中没有值)。

The reason that this is happening is that you read your entire file inside of your first while loop which places the file pointer at the end of the file. Then (without resetting the file pointer), you try to read it line by line in the for loop. Since the file pointer is at the end of the file already, fgetl will always return an empty array ([]) so when you start trying to work with it, you get the indexing error that you've shown.

发生这种情况的原因是您在第一个while循环中读取整个文件,该循环将文件指针放置在文件的末尾。然后(无需重新设置文件指针),尝试在for循环中逐行读取它。由于文件指针已经在文件的末尾,所以fgetl将始终返回一个空数组([]),因此当您开始尝试使用它时,您将得到您已经显示的索引错误。

The solution is one of two options:

解决方案是两种选择之一:

  1. Call frewind(ficheiro) before the for loop to reset the file pointer to the beginning of the file so that you can successfully read each line.

    在for循环之前调用frewind(ficheiro),将文件指针重置为文件的开头,这样就可以成功地读取每一行。

  2. Come up with a better way to parse your file rather than looping through the whole file for the sole purpose of counting the number of lines in the file.

    找到一种更好的解析文件的方法,而不是为了计算文件中的行数而对整个文件进行循环。

If you post some of the file contents we can likely provide you with a better way to parse the file in one or two lines of code.

如果您发布了一些文件内容,我们可能会为您提供一种更好的方法来在一到两行代码中解析文件。

Update

更新

Also if you look at this line, n goes all the way to the end of pontovirgula.

如果你看这条线,n一直到pontovirgula的末端。

for n = 1:2:length(pontovirgula)

But then you try to access 1 and 2 past the end of the array

然后你尝试访问数组结束后的1和2

pontovirgula(n+1)
pontovirgula(n+2)

This is going to cause issues for sure. Try only looping until 2 from the end instead.

这肯定会引起问题。尝试只循环直到2结束。

for n = 1:2:(numel(pontovirgula) - 2)

Update 2

更新2

Given that you have posted the file contents, here is an alternate way to parse the file.

假设您已经发布了文件内容,这里有一种解析文件的替代方法。

fid = fopen('filename.txt', 'rt');
lines = textscan(fid, '%s', 'HeaderLines', 1);
fclose(fid);

% For each line we want to get the reference off of the front
parts = regexp(lines{1}, ';', 'split');

materials = cell(1, numel(parts));
elements = cell(1, numel(parts));
percentages = cell(1, numel(parts));

for k = 1:numel(parts)
    % Remove the last empty blank
    parts{k}(end) = [];

    % Get the material (the first element)
    materials{k} = parts{k}{1};

    % Get the elements
    elements{k} = parts{k}(2:2:end);

    % Get the percents
    percents{k} = parts{k}(3:2:end);
end

#2


0  

Check the length of linha, and the values of pontovirgula that you are using to index into linha.

检查linha的长度,以及您将其用于索引到linha的pontovirgula的值。