matlab的fortran格式等效项 [英] matlab's fortran's format equivalents

查看:97
本文介绍了matlab的fortran格式等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当于matlab的

write(1,'("Speed, resistance, power",3f8.2)')(a(i),i=1,3)

我尝试过

a = [10. 20. 200.]
fprintf(unit1,'a = 3%8.1e',a)

但是我仍然遇到麻烦(整个matlab输出格式化的东西).

but I'm still having trouble with it (the whole matlab output formatting thing).

为肯尼(Kenny)对于上面给出的a的值,它将给出(在新行中):

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

推荐答案

我使用1作为fileID写入命令窗口,并且由于它更漂亮,所以在其末尾添加了换行符,但这应该可以再现您想要的内容

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


编辑

假定一个未知尺寸的数组a.进一步假设我们要逐行打印

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

注意:这会在同一行上打印一个接一个的所有行(感谢@JS).

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')

这篇关于matlab的fortran格式等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆