How do I get fprintf to print horizontally (table) using a for loop?
显示 更早的评论
This is my current code (rawData represents a 2x8 matrix)
for k =1:length(rawData) fprintf('%10.2f',rawData(1,k), rawData(2,k)) fprintf('\n') end
The code creates a chart like this: EX: 15.94 (k=1) 15.96 (k=1) 15.98 (k=2) 15.94 (k=2,etc.)
I would like the data to appear like this however: EX: 15.94 15.98 15.96 15.94 Any suggestions, its driving me insane?
回答(1 个)
dpb
2017-4-24
>> val=reshape(1:16,2,[]).'; % some data so can see order printed vis a vis in memory
>> fmt=[repmat('%8.2f',1,size(val,2)) '\n']; % build appropriate formatting string
>> fprintf(fmt,val.') % and print in row-major order
1.00 2.00
3.00 4.00
5.00 6.00
7.00 8.00
9.00 10.00
11.00 12.00
13.00 14.00
15.00 16.00
>>
No looping needed; just use proper format string and ensure print in proper order. Matlab stores internally by column-major so note the transpose.
类别
在 帮助中心 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!