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.

采纳的回答

Sindar
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
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.
kian taherkhani
kian taherkhani 2020-2-16
yeah you are absolutely right, that makes a lot more sense.
thanks for the responses and sorry for wasting your time lol.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by