- 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:
How do i read 60 .txt files into matlab?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Jan
2022-2-26
编辑:Jan
2022-2-26
Some hints:
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 个评论
Jan
2022-2-26
编辑:Jan
2022-2-26
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!