How do i read 60 .txt files into matlab?

2 views (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 = {liste.name};
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};
end

Answers (1)

Jan
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:
daten{1,n}
daten{1,n}{1,1}
daten{1,n}{1,1}{1,60}
Which one is failing?
  3 Comments
Matthew Worker
Matthew Worker on 26 Feb 2022
i have now tried to change everything accordingly to your suggestions but i still get an error message:
Error using textscan
First input must be a valid file-id or non-empty character vector.
Error in auswertung_enzymkinetik_selbst (line 13)
daten{n} = textscan(fileID,'%s');
path = 'C:\Users\wilma\OneDrive\Dokumente\MATLAB\PCG\Enzymkinetik';
liste_help = dir(path);
liste = liste_help(4:63,:);
files = {liste.name};
for n=1:numel(files)
% einlesen der Dateien
fileID = readlines(fullfile(path,files{n}));
daten{n} = textscan(fileID,'%s');
val{n} = daten{1,n}{1,1},{1,60};
end
My guess is that the textscan doesn't work with the 'readlines' from the fileID, but i don't know what would fix this
Jan
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.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!