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 visualizaciones (últimos 30 días)
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 comentarios
BGN
BGN el 5 de Feb. de 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)
BGN
BGN el 5 de Feb. de 2020
nevermind, I just cleared the workspace and it somehow worked. Thank you all for helping me!

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 5 de Feb. de 2020
Editada: Guillaume el 5 de Feb. de 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;

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by