how to read data from multiple files and save in single file

8 ビュー (過去 30 日間)
sandy
sandy 2013 年 8 月 7 日
コメント済み: Angana Borah 2019 年 9 月 19 日
i have 5000 text files each file having(1*50). i need to read those all files from top to bottom order and want to save them in single text file.which should contain all data from those 5000 files(5000*50).

採用された回答

Jan
Jan 2013 年 8 月 7 日
編集済み: Jan 2013 年 8 月 7 日
This concatenates the files directly without a conversion to doubles and back again:
pathName = 'C:\Temp';
fileList = dir(fullfile(pathName, '*.txt'));
out = fopen(fullfile(pathName, 'Joined.txt'), 'w');
for k = 1:numel(fileList)
s = fileread(fullfile(pathName, fileList(k).name));
fwrite(out, s, 'char');
end
fclose(out);
Perhaps it is needed to care about a trailing line break:
if s(end) =~ char(10) && s(end-1:end) ~= char([13, 10])
fwrite(out, char(10), 'char'); % Or char([13,10]) ?
end
  4 件のコメント
julio martinez
julio martinez 2018 年 10 月 9 日
How could concatenate the files skipping the header row?
Angana Borah
Angana Borah 2019 年 9 月 19 日
Thank you so much. can anyone please tell me how I can understand each line in the code? what 'w'w does and what is the purpose of the for loop logic?

サインインしてコメントする。

その他の回答 (3 件)

David Sanchez
David Sanchez 2013 年 8 月 7 日
my_files = dir('*.txt');
N_files = numel( my_files );
A = zeros( numel(my_files),50 ); % initialize matrix to hold data
for k = 1:N_files
file2read = my_files(k).name;
fid = fopen(file2read);
A(k,:) = fscanf(fid, '%g', [1 inf]); % data from file
fclose(fid);
end
% write data to new file
fid = fopen('new_file.txt', 'w');
fprintf(fid, '%g %g\n', A);
fclose(fid);
  1 件のコメント
Jan
Jan 2013 年 8 月 7 日
The conversion from text to numbers and back again requires a lot of time.

サインインしてコメントする。


KRUNAL
KRUNAL 2014 年 7 月 24 日
編集済み: KRUNAL 2014 年 7 月 24 日
I am trying to do the same as sandy wants to. I am trying to read excel files from two different folders and output it to an excel file present in third folder. Can anyone suggest how can it be done?
P.S : I am reading only a column of data from the two files and not the entire file

hemant vyas
hemant vyas 2018 年 6 月 23 日
sir can we read and write in a video file simultaneously if I am recording video by webcam a saving it in .m file than in the same code i want to process it in matlab2013a

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by