简体   繁体   中英

Plotting a variable in a Legend - Matlab

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.

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 ).

Simply use the native concatenation feature of array definition with [] and num2str :

legend('Type 1', ['Type ' num2str(Type/4, '%i')]);

Best,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM