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)

 采纳的回答

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 个评论

Thanks for the reply. But this will generate the following errors as:
Warning: Escaped character '\R' is not valid. See 'doc sprintf' for supported special characters.
> In linetext (line 3)
Error using fgets
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in fgetl (line 32)
[tline,lt] = fgets(fid);
Error in linetext (line 6)
fgetl(fid); % read and dump
It works but can output the value of subject 8 only. I want to save the result from subject 1 to 8. Thanks for the response.
for l = 1:8
fid=fopen(sprintf('E:\\ReliabilityAll\\alpha\\sub%d.txt',l));
all_results=[];
StartLine=3;
for k=1:StartLine-1
fgetl(fid); % read and dump
end
Fline=fgetl(fid); % this is the 3rd line
all_results1=Fline;
all_results=[all_results;all_results1];
%do stuff
fclose(fid)
end
I suggest
base = 'E:\ReliabilityAll\alpha\sub';
for l = 1 : 8
filename = sprintf('%s%d.txt', base, l);
[fid, msg] = fopen(filename);
if fid < 0
error('Failed to open file "%s" because "%s"', filename, msg);
end
end
Fline = cell(8, 1);
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{l} = fgetl(fid); % this is the 3rd line
%do stuff
fclose(fid)
end
celldisp(Fline)
Thanks @Madhan Ravi It really works and save my time to do it manually. I am pleased to note that it is really a very active platform and grateful to all of you for the kind response
I suggest you to read sir Walter’s comment as well.
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..

类别

帮助中心File Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by