Generating a legend for one function that is associated with two variables
이전 댓글 표시
I am putting together a script that plots projectile motion as a function of the initial velocity and the launch angle. I am trying to add a legend that denotes v0 and theta for the plot and updates with the variables assigned in the command window. When I run the script, I get the legend, but the string message associated with the variable is listed twice. Can anyone provide any suggestions? This is the code I have right now for labeling the plot:
% Plot title and legend
title('2D Projectile Motion')
variableconvert = sprintf('Velocity and theta: %3d', v0, theta);
legend(variableconvert)
댓글 수: 2
dpb
2021년 2월 15일
You asked to output two variables but only gave one output field -- so the whole format string is repeated.
variableconvert = sprintf('Velocity %3d and theta: %3d', v0, theta);
would be one way to fix. Salt to suit...
NB: that the '%d' format will limit you to showing only integer values. If that's all you're using, that's fine; you might consider
variableconvert = sprintf('Velocity and theta: %.1f, %.1f', v0, theta);
as another possibility.
Olivia Kline
2021년 2월 15일
답변 (1개)
The call to sprintf contains one format specification and two values. That applies the formatSpec twice.
demo:
sprintf('Demo %d ', 1,2)
Either add another formatSpec or remove one of the values.
sprintf('Demo %d ', 1)
sprintf('Demo %d %d', 1, 2)
카테고리
도움말 센터 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!