Avoid multiple output using fprintf of multiple variables in livescript

My script is:
k=[3.96306869127352e-05;
1.33047441644794e-05;
3.37761112449900e-05;
7.19380417629400e-06];
confk=[2.95622613104490e-05;
6.38219462217362e-06;
1.15227436682386e-05;
8.98428923906260e-05];
formatSpec = 'k1=%4.2e +/- %8.3e;\n';
f1=fprintf(formatSpec,k(1),confk(1));
k1=3.96e-05 +/- 2.956e-05;
formatSpec = 'k2=%4.2e +/- %8.3e;\n';
fprintf(formatSpec,k(2),confk(2));
k2=1.33e-05 +/- 6.382e-06;
formatSpec = 'k3=%4.2e +/- %8.3e;\n';
fprintf(formatSpec,k(3),confk(3));
k3=3.38e-05 +/- 1.152e-05;
formatSpec = 'k4=%4.2e +/- %8.3e;\n';
fprintf(formatSpec,k(4),confk(4));
k4=7.19e-06 +/- 8.984e-05;
Even if it works that way, what I want is that it comes out one output and not one for each. I tried this way:
formatSpec='k=%4.2e +/- %8.3e\n';
fprintf(formatSpec,k,confk)
k=3.96e-05 +/- 1.330e-05 k=3.38e-05 +/- 7.194e-06 k=2.96e-05 +/- 6.382e-06 k=1.15e-05 +/- 8.984e-05
But so they don’t come out "k1,k2,k3,k4". I also tried with a for cycle but nothing done.

 採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 4 月 19 日
編集済み: Dyuman Joshi 2023 年 4 月 19 日
k=[3.96306869127352e-05;
1.33047441644794e-05;
3.37761112449900e-05;
7.19380417629400e-06];
confk=[2.95622613104490e-05;
6.38219462217362e-06;
1.15227436682386e-05;
8.98428923906260e-05];
formatSpec='k%d=%4.2e +/- %8.3e\n';
fprintf(formatSpec,[(1:numel(k))' k confk]')
k1=3.96e-05 +/- 2.956e-05 k2=1.33e-05 +/- 6.382e-06 k3=3.38e-05 +/- 1.152e-05 k4=7.19e-06 +/- 8.984e-05

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB Report Generator についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by