Multiple text file read i Matlab

조회 수: 2 (최근 30일)
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2020년 7월 10일
댓글: Khan Muhammad Adeel Khan 2020년 7월 10일
I want matlab to read multiple text file. The matlab code to read the single text file and specific line is attached. How to read multiple text from the folder?
fid=fopen('E:\ReliabilityAll\alpha\sub1.txt');
StartLine=3;
for k=1:StartLine-1
fgetl(fid); % read and dump
end
Fline=fgetl(fid); % this is the 3rd line
%do stuff
fclose(fid)

채택된 답변

madhan ravi
madhan ravi 2020년 7월 10일
편집: madhan ravi 2020년 7월 10일
for l = 1:8
fid=fopen(sprintf('E:\\ReliabilityAll\\alpha\\sub%d.txt',l));
StartLine=3;
for k=1:StartLine-1
fgetl(fid); % read and dump
end
Fline=fgetl(fid); % this is the 3rd line
%do stuff
fclose(fid)
end
  댓글 수: 8
madhan ravi
madhan ravi 2020년 7월 10일
I suggest you to read sir Walter’s comment as well.
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2020년 7월 10일
Thanks Sir for the feedback and answers from you both really save my time and energy @Walter Roberson. Could you please suggest me a good platform to learn the coding in Matlab in an effective way or suggest any online open source? I woud be grateful

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

추가 답변 (1개)

Robert
Robert 2020년 7월 10일
편집: Robert 2020년 7월 10일
You might also use a direct loop on the result of using the 'dir' command, if the '*' placeholder is sufficient for your search. Take care to use the transpose operator ' on the result of dir, because you need a row vector of results to for-loop.
sDir = 'C:\Users\Khan\Documents\MATLAB';
for sctFile = dir(fullfile(sDir, 'sub*.txt'))'
fh = fopen(fullfile(sDir, sctFile.name));
% here goes your code
% ...
fclose(fh);
end
Or just dir on the directory, and filter the file names within the loop..

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by