How to load txt files from subfolders
Afficher commentaires plus anciens
There is error as i am trying to load .txt files from subfolders. How can i load all *.txt files using generated function (imptxt(FileName,startRow,endRow))
if true
Files=dir('**/*AVE*.txt'); % looking for txt files with 'AVE' at the beginning
for k=1:length(Files) % k=46
FileNames=Files(k).name
end
ImpFiles=zeros(k,100000);
for i=1:k
ImpFiles(i,:)=imptxt(Files(i).name,1,1); % loading all files in one array using generated function for import
end
and i have such error:
if true
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
if true
Error in imptxt (line 100034)
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'TextType', 'string', 'EmptyValue', NaN,
'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
Error in BN (line 17)
ImpFiles(k,:)=imptxt(Files(k).name,1,1);
% code
end
UPD. I checked and it working but only if i start it inside one of subfolders. Otherwise it shows same error as above
8 commentaires
Image Analyst
le 9 Juil 2018
What is imptxt?
By the way,
dir('**/*AVE*.txt')
will not find the files that start with 'AVE'.
dir('**/AVE*.txt')
will.
Paolo
le 9 Juil 2018
Replace:
FileNames=Files(k).name
with
FileNames=fullfile(Files(k).folder,Files(k).name)
Alexandr Lozak
le 11 Juil 2018
Modifié(e) : Alexandr Lozak
le 11 Juil 2018
Walter Roberson
le 12 Juil 2018
Modifié(e) : Walter Roberson
le 12 Juil 2018
Error in imptxt (line 100034)
Over 100000 lines in a single .m file is... disturbing... from a software engineering viewpoint.
You indicate that it was generated by the import tool. It looks likely to me that you could use readtable() instead.
Alexandr Lozak
le 12 Juil 2018
Modifié(e) : Alexandr Lozak
le 12 Juil 2018
This loop is useless:
for k=1:length(Files)
FileNames=Files(k).name
end
It overwrites FileNames repeatedly.
I'm convinced that the M-file imptxt with more than 100'000 lines of code is rubbish. Which tool did create this function? In any case, something went wrong. I recommend to start from scratch again and create a better tool for importing.
Alexandr Lozak
le 12 Juil 2018
Réponse acceptée
Plus de réponses (1)
Alexandr Lozak
le 12 Juil 2018
Modifié(e) : Alexandr Lozak
le 12 Juil 2018
3 commentaires
Alexandr Lozak
le 12 Juil 2018
Jan
le 12 Juil 2018
It is not safe to change the current folder by cd. Note that any callback of GUIs or timers can change the current folder unexpectedly also. So better use the absolute path. See my answer.
The two loops are not useful. You create the variable "FileNames", but do not use it. So the loop over k is meaningless.
By the way: does this answer solve your problem? If not, accepting it is not useful, because the readers in the forum think, that your question is solved already.
Alexandr Lozak
le 12 Juil 2018
Catégories
En savoir plus sur Whos dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!