using non number vectors and number vectors in the same fprintf command (beginner)

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.

 Accepted Answer

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 Comments

hi, thanks for the reply
see thats what i thought as well but ill copy the questions text so you could see what im working with
A vector is given by x = [9, 4, 0 ,7]. Write a MATLAB program that creates the vector x, then creates a matrix M from this vector.
The matrix M has four rows, as follows:
• The original vector, x
• The reciprocal of x
• The factorial of the elements in x computed using the myFactorial function from the last question
• The factorial of the elements in x computed using MATLAB's factorial function.
Output the matrix M, with a ruler line above and below. For the numeric output, use MATLAB's fprintf function, creating the lines of output in a single statement that executes once. For each value, use a field width of 15 characters, including 4 decimal places.
The following is the desired output:
--------------- --------------- --------------- ---------------
9.0000 4.0000 0.0000 7.0000
0.1111 0.2500 Inf 0.1429
362880.0000 24.0000 1.0000 5040.0000
362880.0000 24.0000 1.0000 5040.0000
--------------- --------------- --------------- ---------------
the formating is a bit off but theres the gist of it.
the only thing i can think of is that what is meant is output the 4 main lines in one statement and im free to print the borders in other statements, and its just worded this way.
I wasnt sure if its just a lack of knowledge on my part, but it seems that that wasnt the case so maybe thats what they meant?
"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.
yeah you are absolutely right, that makes a lot more sense.
thanks for the responses and sorry for wasting your time lol.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!