Create one file from many files

2 ビュー (過去 30 日間)
Ivan Mich
Ivan Mich 2020 年 5 月 22 日
編集済み: Stephen23 2020 年 6 月 2 日
Hello,
I have multiple files, with the following names:
m1,m2,....., m25 (files without extension).
Each file has a number. I would like to create a file which will have all the values vertically of each file I have.
I am importing the files I have and the file I would like to create.
Thank you in advance

回答 (2 件)

Cristian Garcia Milan
Cristian Garcia Milan 2020 年 5 月 22 日
Hi!
You should use
fid = fopen(file(i),'r');
for each file. Then read it. I usually use
value(i) = str2num(char(fread(fid)));
Then you can close the file
fclose(fid);
If you want to write it in a file, you shoul follow a similar way.
fid = fopen(file_to_write,'w');
fprintf(fid,'%f\n',value);
fclose(fid)
Hope it helps!
  3 件のコメント
Cristian Garcia Milan
Cristian Garcia Milan 2020 年 5 月 22 日
try the following code:
files = dir('m*.');
values = zeros(1,length(files));
for i = 1:length(files)
file = files(i).name;
fid = fopen(file,'r');
values(i) = str2num(char(fread(fid)));
fclose(fid);
end
fid = fopen('OutputFile','w');
fprintf(fid,'%f\n',values);
fclose(fid)
Stephen23
Stephen23 2020 年 5 月 22 日
Warning: the code given in the previous comment will not parse the filenames in alphanumeric order. With the filenames given in the original question, that code will parse the files and concatenate the data in this order:
m1
m10
m11
...
m19
m2
m20
m21
...
m25

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


Stephen23
Stephen23 2020 年 5 月 22 日
編集済み: Stephen23 2020 年 5 月 22 日
D = 'absolute/relative path to where the files are saved';
N = 25; % number of files
C = cell(1,N);
for k = 1:N
F = fullfile(D,sprintf('m%u.txt',k));
C{k} = dlmread(F);
end
M = vertcat(C{:});
dlmwrite('final.txt',M,'\t')
See also:
  10 件のコメント
Ivan Mich
Ivan Mich 2020 年 6 月 1 日
編集済み: Ivan Mich 2020 年 6 月 1 日
Excuse me, How could I replace in my code dlmwrite with writematrix?
Sorry to ask you again and again but I am really confused!
Stephen23
Stephen23 2020 年 6 月 1 日
編集済み: Stephen23 2020 年 6 月 2 日
I posted before refreshing this page, so I did not see your comment with code. On the first iteration the outer two loops your code has only created one file, but the nested loop that you added attempts to read twenty-five files, of which twenty-four don't exist. I don't see how that could work.
Get rid of the innermost loop by merging its code in with the outer two loops:
D = 'absolute/relative path to where the files are saved'; % you need to change this!
for ii = 1:numel(st);
N = size(nam,1); % or move to before the loops.
C = cell(1,N);
for jj = 1:N;
fnm = fullfile(D,sprintf('m%g0.txt',jj));
[fid,msg] = fopen(fnm,'wt');
assert(fid>=3,msg)
fprintf(fid,'%s\t',num2str(Results));
fclose(fid);
%
C{k} = dlmread(fnm);
end
mat = vertcat(C{:});
fnm = fullfile(D,sprintf('final%u.txt',ii));
dlmwrite(fnm,mat,'\t')
end
Are all of the separate number files really required? Why not just concatenate the data directly in MATLAB memory and only save the final file?

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

カテゴリ

Help Center および File ExchangeJSON Format についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by