MATLAB Answers

I'm trying to use fprinft in title because when i use the sprintf function it has way too many decimals. I keep getting error messages and i dont know how to fix them

4 views (last 30 days)
BGN
BGN on 5 Feb 2020
Commented: BGN on 5 Feb 2020
subplot (2,2,1)
plot(X,Y)
z2= fprintf('Trajectory of projectile(v0= %2.0f\n and angle %2.0f\n)', v0, theta);
title(z2)
xlabel('Position in X direction (m)')
ylabel('Position in Y direction (m)')
Index exceeds the number of array elements (1).
Error in assigment03_pt01 (line 40)
title(z2)

  4 Comments

Show 1 older comment
Rik
Rik on 5 Feb 2020
In addition to the points made by Stephen: the error hints that you have a scalar variable with the name title. Can you post the output of the following line of code to confirm this?
clc,which title -all
BGN
BGN on 5 Feb 2020
title is a variable.
C:\Program Files\MATLAB\R2019b\toolbox\matlab\graph2d\title.m % Shadowed
Index exceeds the number of array elements (1).
Error in assigment03_pt01 (line 41)
title(z2)
(i dont know how i made it a variable though)

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 5 Feb 2020
Edited: Guillaume on 5 Feb 2020
"I keep getting error messages"
Then read the doc of the functions you use, in particular pay close attention to what the inputs and outputs are.
For this particular error:
Index exceeds the number of array elements (1).
Error in assigment03_pt01 (line 40)
title(z2)
it would be because you have created a variable named title, so title(z2) is no longer passing z2 to the title function but instead is indexing into your title variable.
With regards to sprintf vs fprintf, as Stephen says you can't swap one for the other and in any case they format text exactly the same way (hence the very similar names). You want sprintf. Read the documentation of the format string to customise the display to what you want. Perhaps you want %g instead of %f.
"(i dont know how i made it a variable though)"
Somewhere, you wrote
title = something;

  0 Comments

Sign in to comment.

Sign in to answer this question.