Borrar filtros
Borrar filtros

Not sure why I'm getting this error - Index exceeds number of array elements (0).

3 visualizaciones (últimos 30 días)
I'm not quite understanding why I am getting this error for the code below. I get the error
Index exceeds the number of array elements (0).
Error in importdata (line 36)
alpha(k,1) = first_data(1);
However I only get this error when the number of files I am trying import exceeds 10. I'm guessing there is something I am missing but I can't quite put my finger on it. Can anyone suggest a way to solve it?
% Read the data from each file
fid = fopen(fullFileName);
c = 0; store = false;
while true
l = fgetl(fid);
if l == -1 % Break loop
break
end
% while ~feof(fid)
% Read Alpha and Beta
if strcmp(l(1:5),'alpha') %Get row starting with alpha
l = fgetl(fid); % get next line
first_data = str2num(l);
%alpha_beta(k,1:2) = first_data([1,3]); % store alpha and beta
alpha(k,1) = first_data(1);
beta(k,2) = first_data(3);
end
% Read matrix of data below 'Dose'
store = strcmp(l(1:4),'Dose');
while store
l = fgetl(fid);
c=c+1;
if l == -1 % Exit and set store back to false
store = false;
break
end
data(c,:) = str2num(l);
end
end
file{k} = data;
clear data
%Read 'Dose', 'SF', 'Errors' into separate cells
dose{k} = file{k}(:,1);
sf{k} = file{k}(:,2);
numcol = @(x) size(x,2);
if numcol(file{k}) == 3
errs{k} = file{k}(:,3)-sf{k};
else
sfanderrs{k} = file{k}(:,3);
errs{k} = file{k}(:,4);
end
end

Respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 29 de Dic. de 2019
alpha(k) = first_data(1);
beta(k) = first_data(3);
  3 comentarios
mel1708
mel1708 el 29 de Dic. de 2019
The purpose of first_data is to read a row of 8 values from each .dat file read. The code appears to work up until I am working with more than 10 .dat files.
Name Size Bytes Class Attributes
first_data 1x8 64 double
I realised I didnt copy the start of the code where I am importing files with a certain sequence in the filename. The section of code below should be placed before %Read data from each file in my original question.
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*235*'); % Define desired parameters
theFiles = dir(filePattern); % List the files which satisfy these parameters
whichFile = fullfile(myFolder,{theFiles.name});
numFiles = length(theFiles);
for k = 1 : numFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Read the data from each file
fid = fopen(fullFileName);
c = 0; store = false;
while true
l = fgetl(fid);
if l == -1 % Break loop
break
end

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by