using non number vectors and number vectors in the same fprintf command (beginner)
1 回表示 (過去 30 日間)
古いコメントを表示
hey guys, ive been at this for the past hour something but i can not figure it out, so i thought ill ask it over here. for my homework i need to display output using fprint f with some borders, but its proving beyond my problem solving abilities.
so i have this code
x=[9 4 0 7];
x2=1./x;
x3=myFactorial(x);
x4=factorial(x);
fprintf('%15.4f %15.4f %15.4f %15.4f\n', x',x2',x3',x4')
which is all good (dont worry about x3 its just from a previous q), but i need to add a border like this :----------- to the top.
the closest ive come is with the command fprintf('--------------------\n %15.4f %15.4f %15.4f %15.4f\n', x',x2',x3',x4'), which gives off a weird
--------------------
9.0000 4.0000 0.0000 7.0000
--------------------
0.1111 0.2500 Inf 0.1429
--------------------
362880.0000 24.0000 0.0000 5040.0000
--------------------
362880.0000 24.0000 1.0000 5040.0000
without the \n after the ---- it does the same thing but with the numbers on the same level as the ---- in the output. ive tried many things like putting the ----- phrase in a vector and then putting the vector in the fprint command, but that simply gives me no output. question is how can i get something like this
--------------------------------------------------------------------------------
9.0000 4.0000 0.0000 7.0000
0.1111 0.2500 Inf 0.1429
362880.0000 24.0000 0.0000 5040.0000
362880.0000 24.0000 1.0000 5040.0000
in only one fprintf command line.
Sorry if its a beginner question but ive searched many forums and didnt find anyone else having the same problem, as it can be simply solved by multiple fprintf command but i have my limitations in the actualy question.
0 件のコメント
採用された回答
Sindar
2020 年 2 月 16 日
編集済み: Sindar
2020 年 2 月 16 日
There is no good reason to do this in a single fprintf command, and the workaround necessary to do it would be painful. Do this:
fprintf('--------------------\n')
fprintf('%15.4f %15.4f %15.4f %15.4f\n', x',x2',x3',x4')
The reason this is hard is not that you are mixing numbers and text, but that you want a header line different from the rest. fprintf is not designed to do this. If the HW specifies you need it in one fprintf command, give us the exact wording and we may be able to figure out (hopefully) what they are actually asking for or at least why
3 件のコメント
Sindar
2020 年 2 月 16 日
編集済み: Sindar
2020 年 2 月 16 日
"For the numeric output, use MATLAB's fprintf function, creating the lines of output in a single statement that executes once."
The non-numeric output doesn't have to be done in this single statement. It could definitely have been worded more clearly.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!