How do I get fprintf to print horizontally (table) using a for loop?
Show older comments
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?
Answers (1)
dpb
on 24 Apr 2017
>> 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.
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!