column-wise input for string and double data using fprintf

조회 수: 8 (최근 30일)
sermet OGUTCU
sermet OGUTCU 2021년 11월 19일
댓글: VBBV 2021년 11월 19일
data_string=
32×3 char array
'G01'
'G02'
'G03'
.
'G32'
data_numeric= 32 x 6 double
fprintf('%s %.3f %.3f %.3f %.3f %.3f %.3f\n', data_string.', data_numeric.')
It writes the data_string and data_numeric in a text file as the following structure:
G01G02G03G04G05G06G07G08G09G10G11G12G13G14G15G16G17G18G19G20G21G22G23G24G25G26G27G28G29G30G31G32 . . . (along with 5 numeric values in the same line)
27 x 6 numeric values (after the first line)
How I can print the data_string and data_numeric as column-wise (7 x 32) in a text file? For example the output should be like this:
G01 . . . . . .
G02 . . . . . .
. . . . . . .
G32 . . . . . .

채택된 답변

VBBV
VBBV 2021년 11월 19일
fprintf('%s\n %.3f\n %.3f\n %.3f\n %.3f\n %.3f\n %.3f\n', data_string.', data_numeric.')
Add a newline after each format specifier
  댓글 수: 2
sermet OGUTCU
sermet OGUTCU 2021년 11월 19일
it produced the irrelevant format such as:
G01G02G03G04G05G06G07G08G09G10G11G12G13G14G15G16G17G18G19G20G21G22G23G24G25G26G27G28G29G30G31G32
0.809
1.231
.
.
VBBV
VBBV 2021년 11월 19일
data_string={'G01','G02','G03','G04','G05','G06'}
data_string = 1×6 cell array
{'G01'} {'G02'} {'G03'} {'G04'} {'G05'} {'G06'}
data_numeric= 1.4*rand(1,6)
data_numeric = 1×6
1.0413 0.6852 0.6532 0.5060 0.9655 0.5505
for i = 1:length(data_string)
fprintf('%s %.3f %.3f %.3f %.3f %.3f %.3f\n', data_string{i}, data_numeric)
end
G01 1.041 0.685 0.653 0.506 0.966 0.551 G02 1.041 0.685 0.653 0.506 0.966 0.551 G03 1.041 0.685 0.653 0.506 0.966 0.551 G04 1.041 0.685 0.653 0.506 0.966 0.551 G05 1.041 0.685 0.653 0.506 0.966 0.551 G06 1.041 0.685 0.653 0.506 0.966 0.551
try like this

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by