Create one file from many files

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

Respuestas (2)

Cristian Garcia Milan
Cristian Garcia Milan el 22 de Mayo de 2020
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 comentarios

Ivan Mich
Ivan Mich el 22 de Mayo de 2020
command window shows me:
Undefined function or variable 'file'.
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 el 22 de Mayo de 2020
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

Iniciar sesión para comentar.

Stephen23
Stephen23 el 22 de Mayo de 2020
Editada: Stephen23 el 22 de Mayo de 2020
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 comentarios

Ivan Mich
Ivan Mich el 30 de Mayo de 2020
Hello,
Sorry to annoy you again. I have a question. Well I have created a code which use a loop with two iterations. Each iteration creates 25 .txt files (as I said previously) , and with the use of the above code I megre them in one file. But I would like to create one merged file after each Iteration. Do you know how yo make it?
I wrote this:
for n=1:numel(element);
.......
FP=fopen(sprintf('m%g0.txt',i),'wt');
fprintf(FP,'%s\t',num2str(results));
fclose(FP);
end
How could I put in my code your suggested script ?
Thank you in advance
Stephen23
Stephen23 el 30 de Mayo de 2020
Editada: Stephen23 el 30 de Mayo de 2020
"Well I have created a code which use a loop with two iterations. Each iteration creates 25 .txt files (as I said previously)"
It is unclear what you mean by "iteration": if your loop creates one file on each loop iteration, then it would require 25 iterations to create 25 files (that would also match with the example code you have shown).
I suspect that you might need nested loops, but this is just a guess.
On second reading, perhaps you want to append data to a file, where each loop iteration simply appends new data to the data saved on previous iterations. Once again, just a guess, because your description is not very clear. Please explain with a simple example and explanation in different words and I will endeavor to help you more.
Ivan Mich
Ivan Mich el 30 de Mayo de 2020
look, my code works with two for loops. The first for is reading two files.
for n=1:numel(name);
the second for loop creates after some calculations 25 .txt files. with your code:
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')
I create 1 .txt file which merge all the 25 .txt files. This is for the first iteration.
But when my code reads the second file and makes the calculations that I want overwrittes the previous "final.txt" file with the new data. I would like then to create a different "final.txt" file and keeping the previous one , without overwriting my data.
That's what I meant with my question.
Could you help me?
Stephen23
Stephen23 el 31 de Mayo de 2020
Editada: Stephen23 el 31 de Mayo de 2020
You can easily change the filename of final.txt, e.g.:
for n = 1:numel(name);
...
FF = sprintf('final%u.txt',n);
dlmwrite(FF,M,'\t')
end
Ivan Mich
Ivan Mich el 1 de Jun. de 2020
My command window shows:
Error using dlmread (line 62)
The file 'C:\Users\HP\Desktop\exp\m2.txt' could not be opened because: No such file or directory
C{k} = dlmread(F);
Do you know why?
Stephen23
Stephen23 el 1 de Jun. de 2020
Apparently that file does not exist in that location. You can print a list of the existing files:
ls C:\Users\HP\Desktop\exp
Ivan Mich
Ivan Mich el 1 de Jun. de 2020
Editada: Ivan Mich el 1 de Jun. de 2020
Yes, but the point is that I want first to create the files m1,m2,...,m25.txt and secondly to create the final1.txt, final2.txt with your solution.
According to the above what corrections I have to do in my code? In order to separate the error
Ivan Mich
Ivan Mich el 1 de Jun. de 2020
I mean something is going wrong with the loop:
for n=1:numel(st);
for i=1:size(nam);
FP=fopen(sprintf('m%g0.txt',i),'wt');
fprintf(FP,'%s\t',num2str(Results));
fclose(FP);
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{:});
FF = sprintf('final%u.txt',n);
dlmwrite(FF,M,'\t')
end
end
Ivan Mich
Ivan Mich el 1 de Jun. de 2020
Editada: Ivan Mich el 1 de Jun. de 2020
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 el 1 de Jun. de 2020
Editada: Stephen23 el 2 de Jun. de 2020
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?

Iniciar sesión para comentar.

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Preguntada:

el 22 de Mayo de 2020

Editada:

el 2 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by