Merging data for output

I have a 1 x 10681 double, f, and a 10681 x 12 double, fdat. I want to create a .txt or .dat output file with f and fdat merged and aligned into 13 columns. How do I do this?

Risposte (1)

Star Strider
Star Strider il 5 Dic 2018
It depends on what you want.
This creates the vector as the column of the resulting matrix:
f = rand(1, 10681);
fdat = rand(10681,12);
Out = [fdat,f(:)];
the easiest way to write the matrix would likely be with the dlmwrite (link) function.

2 Commenti

Star Strider
Star Strider il 5 Dic 2018
AstroLandfield’s Answer moved here:
I got it to work by doing:
fid = fileopen("filename','w')
for a = 1:size(fdat,1);
fprintf(fid, '%6.5f , ',f(1,a))
for b = 1:size(fdat,2);
fprintf(fid, '%6.5f , ',fdat(a,b));
end
fprintf(fid,'\n\r');
end
fclose(fid);
But I was wondering if there was a better way. I got it to work by doing:
fid = fileopen("filename','w')
for a = 1:size(fdat,1);
fprintf(fid, '%6.5f , ',f(1,a))
for b = 1:size(fdat,2);
fprintf(fid, '%6.5f , ',fdat(a,b));
end
fprintf(fid,'\n\r');
end
fclose(fid);
But I was wondering if there was a better way.
I believe there is. Here is how I would do it:
Out = [fdat,f(:)];
fid = fopen('MyFile.txt', 'wt');
fprintf(fido, [repmat('%6.5f ', 1, 12) '%6.5f\n'], Out');
fclose(fid);
Transpose the ‘Out’ matrix (using the (') operator) to write the matrix correctly in the fprintf call. This writes the matrix as I created it.
I have no idea what you are doing with the for looops, since you do not need them to write the file. You would need them if you were writing different data types (such as a string and a numeric value in each row). You do not need them to write a numeric array.

Accedi per commentare.

Categorie

Richiesto:

il 5 Dic 2018

Commentato:

il 5 Dic 2018

Community Treasure Hunt

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

Start Hunting!

Translated by