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개)

Star Strider
Star Strider 2018년 12월 5일

0 개 추천

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

Star Strider
Star Strider 2018년 12월 5일
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에 대해 자세히 알아보기

태그

질문:

2018년 12월 5일

댓글:

2018년 12월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by