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?

回答(1 个)

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 个评论

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.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by