필터 지우기
필터 지우기

How to load txt files from subfolders

조회 수: 2 (최근 30일)
Alexandr Lozak
Alexandr Lozak 2018년 7월 9일
댓글: Alexandr Lozak 2018년 7월 12일
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
Jan
Jan 2018년 7월 12일
편집: Jan 2018년 7월 12일
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
Alexandr Lozak 2018년 7월 12일
I used Matlabs deafault import tool. 100 000 comes from number of values in my .txt files. biggest part of function code looks like this:
%%Format for each line of text:
% column1: double (%f)
...
% column100000: double (%f)

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2018년 7월 12일
편집: Jan 2018년 7월 12일
Files = dir('**/AVE*.txt');
for k = 1:length(Files)
FileName = fullfile(Files(k).folder, Files(k).name);
ImpFiles(k,:) = dlmread(FileName);
end
It is smarter to run the code in backward direction:
for k = length(Files):-1:1
Then ImpFiles has the correct size in the first iteration and Matlab does not waste time with the iterative growing of the array.
  댓글 수: 1
Alexandr Lozak
Alexandr Lozak 2018년 7월 12일
Thank you for answer and explanation

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alexandr Lozak
Alexandr Lozak 2018년 7월 12일
편집: Alexandr Lozak 2018년 7월 12일
This code worked for me. It reads all txt files with 'AVE' in the beginning. Then takes exact name of file and change directory to read the file.
Files=dir('**/AVE*.txt');
for k=1:length(Files)
for i=1:k
FileNames=fullfile(Files(k).folder,Files(k).name);
cd (Files(i).folder);
ImpFiles(i,:)=dlmread(Files(i).name);
end
end
  댓글 수: 3
Jan
Jan 2018년 7월 12일
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
Alexandr Lozak 2018년 7월 12일
Thank you, Jan. Your answer and explanations better than mine. Could you give me hint. How can i load those files directly and in 10 different arrays. I have 10 folders with 5-3 txt files inside. How i can load them in similar way?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by