Vasco Martins
Vasco Martins

Reputation: 11

How to fix error: "A(I): index out of bounds; value 1 out of bound 0"

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

error: called from LOADC at line 15 column 13

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

Upvotes: 0

Views: 7996

Answers (2)

Suever
Suever

Reputation: 65460

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.

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.

  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.

for n = 1:2:length(pontovirgula)

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

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

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

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

Update 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

Upvotes: 3

ramziabbyad
ramziabbyad

Reputation: 56

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

Upvotes: 0

Related Questions