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?

Antworten (1)

Star Strider
Star Strider am 5 Dez. 2018

0 Stimmen

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 Kommentare

Star Strider
Star Strider am 5 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Large Files and Big Data finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 5 Dez. 2018

Kommentiert:

am 5 Dez. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by