How do i read 60 .txt files into matlab?

1 view (last 30 days)
Matthew Worker
Matthew Worker on 26 Feb 2022
Edited: Jan on 26 Feb 2022
I am trying to read experimental data in .txt format into matlab but so far, i get the following error message:
Index in position 2 exceeds array bounds (must not exceed 1).
Error in auswertung_enzymkinetik_selbst (line 15)
abs{n} = daten{1,n}{1,1}{1,60};
My guess is that the second index in the last curly brackets is the error but i don't know how to fix it. My code this far:
clear all
close all
%% Auswertung zum Versuch Enzymkinetik
path = 'C:\Users\wilma\OneDrive\Dokumente\MATLAB\PCG\Enzymkinetik';
liste_help = dir(path);
liste = liste_help(4:63,:);
files = {};
for n=1:numel(files)
% einlesen der Dateien
fileID = fopen(fullfile(path,files{n}),'r');
tline = fgetl(fileID);
daten{n} = textscan(tline,'%s');
abs{n} = daten{1,n}{1,1}{1,60};

Answers (1)

Jan on 26 Feb 2022
Edited: Jan on 26 Feb 2022
Some hints:
  • Omit the clear all . It wastes time and has no benefit. It removes all loaded functions from the memory and relaoding them from the slow disk takes a lot of time.
  • Do not sahdow the important function path by a local variable. This mightwork usually, but can drill a hole in your need during debugging. "abs" is a function also and using it as variable can cause unexpected effects.
  • Are you sure you want to import the first line of the files only? tline = fgetl(fileID); This read one line. Maybe you want daten{n} = textscan(fileID,'%s') instead.
  • Opening files by fopen without closing them by fclose works for some time, until the number of simultaneously opened files is exceeded. So care for closing files as soon as possible in the code.
  • Use the debugger to examine the cause of problems. Type this in the command window and run the code again:
dbstop if error
Now check, what the data are:
Which one is failing?
Jan on 26 Feb 2022
Edited: Jan on 26 Feb 2022
readlines imports the contents of the file already. It replaces textscan.
Again: You can examine such problems using the debugger. Read the documentation also for failing commands:
help readlines
It is still not clear, what the purpose of the nested indexing in "daten{1,n}{1,1},{1,60}" is. Do you want to import the 60th line, or column, or the 1st 60 lines? Because this lines fails, we cannot guess, what it should do instead.
In "liste = liste_help(4:63,:);" you ignore the first 3 files. Remember, that it is not documented, that the output of dir is sorted alphabetically. Therefore it is fragile to omit the 1st 3 files.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by