0

I am writing a function in MATLAB and need to put a variable range of numbers in an fprintf. I need it to start at 1 and go to the length of a matrix. However what I think should work is not giving me consecutive numbers.

A=[1 2 3; 4 5 6; 7 8 9]
b=[2;4;6]
d=length(A)
k=1:d

Then I insert my function for the answer I want, p.

fprintf(‘The solution is x%i equals %f \n’, k, p)

In the output response it’s not giving me the correct k value which should be “1” “2” or “3”. The answer itself is correct if I leave the “x%i equals” out.

ETA: Right now I’m getting:

The solution is x1 equals 2.0000
The solution is x3 equals -0.6667
The solution is x1.333333e+00 equsls 0.000

If I leave out the x%i, I get:

The solution is -0.6667
The solution is 1.3333
The solution is 0.0000
Kristin
  • 1
  • 1
  • 1
    Welcome to Stack Overflow! Can you please [edit] your question with the current output of the code and the desired output, i.e. as full strings? As a side-note, it's highly recommended to not use `ans` as a variable name (which MATLAB should warn you about). – Adriaan Sep 28 '22 at 11:59
  • 1
    I don't understand what exactly are you trying to achieve, what's your desired output? Meanwhile, look at this [post](https://stackoverflow.com/questions/14924181/how-to-display-print-vector-in-matlab) for some ideas – kikon Sep 28 '22 at 12:02
  • I want it to say “The solution is x1 equals” “the solution is x2 equals” with the answers inserted. And to do it for whatever size matrix I input as A. A won’t always be a 3x3 matrix. – Kristin Sep 28 '22 at 13:04
  • You show how to construct A, b, d, and k, but then you want to print p? What is p? Please read [mre], then [edit] your post again accordingly. It really is a lot easier for us to help you if you make it clear what you’re doing and what you want to accomplish. – Cris Luengo Sep 28 '22 at 14:05
  • That said, if I understand your problem correctly, the easiest way to accomplish your desired output is to write a loop. Don’t shy away from loops just because it’s MATLAB. – Cris Luengo Sep 28 '22 at 14:06

1 Answers1

0

In your example, k is a vector of three element 1:3 and that is the first variable printed. Your fprintf(etc) is simply printing 1,2,3 using your format string. That's why you get ... x1 equals 2.0000 on the first line, and why the second line starts with ... x3. The 1 goes with the %i, the 2 goes with the %f, and then the format is recycled and the 3 goes with the %i again.

It looks like you want k to be the index of the printed variables and p are the associated values, and k and p have the same number of elements. If that is the case then you could stack them into a single variable as rows and print that out using your format. E.g., try this if p is a row:

fprintf('The solution is x%i equals %f \n', [k;p])

If p is a column then reshape it to a row first:

fprintf('The solution is x%i equals %f \n', [k;p.'])

By stacking the k and p as rows of the same matrix, the values will be printed in column order and you will get the output you want.

James Tursa
  • 2,242
  • 8
  • 9