GYSHIDO
GYSHIDO

Reputation: 33

How to merge two text files in Matlab and duplicate the resulting file?

I need to generate a number of text (or dat) files to be fed into another software. I have two text files (aa.txt and bb.txt) and an equation that generates random numbers.

The produced text (or .dat) file is to be comprised of three parts:

1- The contents of aa.txt. 2- Randomly generated numbers. 3- the contents of bb.txt.

the files' contents are :

aa.txt -->

first file ,,, first line

First file ,,, second line

1234.1234

bb.txt -->

second file ,,, first line

second file ,,, second line

6789.6789

I wrote the following code, but it will only produce one file with the contents of the first source file (aa.txt). Why do I end up with 1 file? Why is the variable A not written in the generated file?

NOF =3;                     % start with a goal to produce 3 files (small scale)
for ii = 1:NOF        
ffid= fopen ('aa.txt','r');    % open the first source file (aa.txt), the idntifier is ffid
df = fopen (['file' sprintf('%d',ii) '.txt'], 'a'); % open a new (destination) file the identifier is df
line=fgets (ffid);            % Read line from first source file

while ischar (line) 
    fprintf ('%s\n',line);
    line =fgets (ffid);
    fprintf (df , line);    % write the newly-read line from first file to the destination file
end
fclose (ffid);               % closing the first source file

A=randn(2,2); % this is just a randonly generated value for checking purposes and will be replaced later with a many sets of equations
save (['file' sprintf('%d',ii) '.txt'],'A', '-append');


sfid=fopen ('bb.txt','r');      % open the second source file, the idntifier is sfid
line2=fgets (sfid);             % Read line from source file

while ischar (line2)
    fprintf ('%s\n',line2);
    line2 =fgets (sfid);
    fprintf (df , line2);
end
fclose (sfid);                  % closing the first source file
end
fclose (df);
fclose('all');

Upvotes: 2

Views: 110

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

This should basically produce what you are looking for:

for ii = 1:3
    % Create the output file...
    fid_out = fopen(['file' num2str(ii) '.txt'],'w');

    % Read the whole content of the first file into the output file...
    fid_aa = fopen('aa.txt','r');

    while (~feof(fid_aa))
        fprintf(fid_out,'%s\n',fgetl(fid_aa));
    end

    fclose(fid_aa);

    % Generate the random matrix and write it to the output file...
    random = cellstr(num2str(randn(2)));

    for jj = 1:numel(random)
        fprintf(fid_out,'%s\n',random{jj});
    end

    % Read the whole content of the second file into the output file...
    fid_bb = fopen('bb.txt','r');

    while (~feof(fid_bb))
        fprintf(fid_out,'%s\n',fgetl(fid_bb));
    end

    fclose(fid_bb);

    % Finalize the output file...
    fclose(fid_out);
end

Given, for example, the file aa.txt with the following content:

A - Line 1
A - Line 2
A - Line 3

and the file bb.txt with the following content:

B - Line 1
B - Line 2
B - Line 3

the output files would present the structure below:

A - Line 1
A - Line 2
A - Line 3
0.18323     0.94922
-1.0298     0.30706
B - Line 1
B - Line 2
B - Line 3

For the sake of optimization, since I/O is very expensive, I suggest you to read the content of aa.txt and bb.txt only once outside the loop that produces the output files, and save their content into cell arrays. Here is how:

fid = fopen('file.txt','r');
data = cell(0);

while (~feof(fid))
    data{end+1} = fgetl(fid);
end

Inside the loop that produces the output files, you can then iterate the cell array contents in order to print them:

for jj = 1:numel(data)
    fprintf(fid_out,'%s\n',data{jj});
end

Upvotes: 1

Related Questions