matlab's fortran's format equivalents
What would be matlab's equivalent of
write(1,'("Speed, resistance, power",3f8.2)')(a(i),i=1,3)
I've tried
a = [10. 20. 200.]
fprintf(unit1,'a = 3%8.1e',a)
but I'm still having trouble with it (the whole mat开发者_StackOverflow中文版lab output formatting thing).
Edit for Kenny: for the values of a as given above, it would give (in a new row):
Speed, resistance, power 10.00 20.00 200.00
I'm using 1 for fileID to write to the command window, and I put a newline in the end because it's prettier, but this should reproduce what you want
a = [10,20,200;20,30,300];
fprintf(1,'Speed, resistance, power%8.2f%8.2f%8.2f\n',a')
Speed, resistance, power 10.00 20.00 200.00
Speed, resistance, power 20.00 30.00 300.00
EDIT
Assume an array a
of unknown dimensions. Assume further that we want to fprint it row by row
a = [10,20,200;20,30,300];
%# find number of columns
nCols = size(a,2);
%# create format string for fprintf. Use repmat to replicate the %8.2f's
fmtString = ['Speed, resistance, power',repmat('%8.2f',1,nCols),'\n'];
%# print
fprintf(1,fmtString,a')
Speed, resistance, power 10.00 20.00 200.00
Speed, resistance, power 20.00 30.00 300.00
Note: This prints all rows of a one after the other on the same line (thanks, @JS).
fprintf('Speed, resistance,power')
fprintf('%8.2f',a')
fprintf('\n')
精彩评论