writing a matrix of data contained in multiple file into a text file and saving as new text files

1 view (last 30 days)
I have a folder containing 5000 text files having 48*3 data set as the content. I haev a text file where i ant to overwrite the content of these 5000 files individually to generate a new set of output files.

Answers (1)

Nilanshu Ranjan
Nilanshu Ranjan on 3 Nov 2022
I understand that you want to perform two tasks. First, you want to combine the data from 5000 text files into a single text file, perform some operations and then split the text file into new output files.
For both the tasks, you can use fprintf() function. Remember to open and close files using fopen() and fclose() . When using fopen() on a file that is not present in the path, MATLAB will create a new file.
A = [1 2; 3 4];
fid = fopen('file.txt','w');
fprintf(fid,'%d %d \n',A);
fclose(fid);
If you want to override textfiles you may use writematrix() function.
For more information check the documentation on fprintf(), writematrix(), fopen(), fclose().

Community Treasure Hunt

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

Start Hunting!