fprintf for 3D matrix

조회 수: 3 (최근 30일)
sermet OGUTCU
sermet OGUTCU 2021년 11월 11일
답변: KSSV 2021년 11월 11일
matrix_2D= 32 x 7 % double
fprintf(fid, '%*s\n', 15, '===== Starts with header=');
fprintf(fid, '\n');
fprintf(fid,'%*s %*s %*s %*s %*s %*s %*s\n',1,'PRN',1,'Radial RMS(cm)', 1, 'Along-Track RMS(cm)',1,'Cross-Track RMS(cm)',1,'Radial STD(cm)',1,'Along-Track STD(cm)',1,'Cross-Track STD(cm)');
fprintf(fid,'%02d %.3f %.3f %.3f %.3f %.3f %.3f\n', matrix_2D.');
I use the above codes for printing matrix_2D in a text file without any problem. Now I have 3D matrix as follows:
matrix_3D= 286 x 5 x 32 %double
I need to print this "matrix_3D" in a text file with the same row, column and slices order. I tried the below codes but the row, column and slices order cannot be kept.
fprintf(fid, '%*s\n', 15, '===== Starts with header=');
fprintf(fid, '\n');
fprintf(fid,'%*s %*s %*s %*s %*s\n',1,'Second',1,'PRN',1,'Radial (cm)', 1, 'Along-Track (cm)',1,'Cross-Track (cm)');
fprintf(fid,'%d %02d %.3f %.3f %.3f\n', matrix_3D);
How I can print matrix_3D in a text file that keeping the same order of matrix_3D (286 x 5 x 32)? My matlab version is 2019a.

채택된 답변

KSSV
KSSV 2021년 11월 11일
A = rand(3,3,3) ;
C = permute(A,[1 3 2]);
C = reshape(C,[],size(A,2),1) ;
fid = fopen('test.txt','w') ;
fmt = [repmat('%f ',1,3),'\n'] ;
fprintf(fid,fmt,C') ;
fclose(fid)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by