Reputation: 37
Hi I am trying to plot a legend using a combination of text and variables.
I did some research and found that sprintf can be used, however I am having trouble implementing it.
My current code is
legend('Type 1','Type' sprintf('%f',Type/4));
'Type' (the variable) is currently set to twenty, so I am trying to get the label to read 'Type 5'. Any help would be much appreciated.
Upvotes: 0
Views: 699
Reputation: 5188
Simply use the native concatenation feature of array definition with []
and num2str
:
legend('Type 1', ['Type ' num2str(Type/4, '%i')]);
Best,
Upvotes: 1
Reputation: 919
You need to write it like this:
legend('Type 1', sprintf('Type %i', Type/4));
If the type is an integer, use %i
instead of %f
or else you will get Type 5.00
(although you could explicitly tell it not to by specifying %.0f
).
Upvotes: 1