how to merge multiple files ?

10 ビュー (過去 30 日間)
Lilya
Lilya 2023 年 3 月 17 日
コメント済み: Lilya 2023 年 3 月 17 日
Hi- I want to merge multiple files into one file by selecting one of those files specifically to be the header
I've tried the following code, which I found in the Matlab community. But the first line is not the header I want.
the name of the header file is: header.405
any help will be appreciated
projectdir = uigetdir('pub/eph/planets/ascii/de405');
dinfo = dir( fullfile(projectdir, '*.405') );
filenames = fullfile( projectdir, {dinfo.name} );
output='merged.txt';
fidout=fopen(output,'w');
for cnt=1:length(dinfo)
fprintf(fidout,'%s\n',dinfo(cnt).name);
fidin=fopen(dinfo(cnt).name);
fwrite(fidout,fread(fidin));
fprintf(fidout,'\n');
fclose(fidin);
end
fclose(fidout);

採用された回答

Antoni Garcia-Herreros
Antoni Garcia-Herreros 2023 年 3 月 17 日
Hello Lilya,
I'm guessing the problem is your header.405 is not the first file listed in dinfo. What you could do i find the position of your header file and set it to be the first.
Index = find(contains(filenames,'header')); % Find the position of the header file
F=[filenames(Index),filenames];
F(Index+1)=[];
output='merged.txt';
fidout=fopen(output,'w');
for cnt=1:length(F)
fprintf(fidout,'%s\n',F(cnt));
fidin=fopen(F(cnt));
fwrite(fidout,fread(fidin));
fprintf(fidout,'\n');
fclose(fidin);
end
fclose(fidout);
Hope this works!
  1 件のコメント
Lilya
Lilya 2023 年 3 月 17 日
thanks Sir Antoni
this error appeard
Error using fprintf
Function is not defined for 'cell' inputs.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by