Printing functions using fprintf
8 ビュー (過去 30 日間)
古いコメントを表示
The prompt for the question has the user input vectors x y and scalar h, then compute some functions I have defined... The problem I'm having is, in the main script i have to print the row vectors x+y and z, one vector per line, using two decimal places for each element.
I have tried numerous fprintf statements such as
"fprintf ('x+y=%.2f+%.2f,x,y)
fprintf ('.%.2f +%.2f',x,y)
fprintf ('%.2f+%.2f=',x+y)"
etc...
however, while each element is rounded to two decimal places, and the addition is simple, it is not printing the way it is required. Any tips/help? Thank you!
0 件のコメント
回答 (1 件)
Ameer Hamza
2020 年 11 月 4 日
If x and y are vectors then try this
x = 1:5;
y = 2:6;
fprintf('%.2f + %.2f\n', [x(:) y(:)].')
Result
1.00 + 2.00
2.00 + 3.00
3.00 + 4.00
4.00 + 5.00
5.00 + 6.00
4 件のコメント
Ameer Hamza
2020 年 11 月 4 日
I think that the current results should be fine and fulfills the requirement. datatable you shared does not follow the rule for printing 2 decimal places.
参考
カテゴリ
Help Center および File Exchange で Low-Level File I/O についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!