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

14 views (last 30 days)
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
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;

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!