
Provide a row vector as legend
8 views (last 30 days)
Show older comments
Hi,
I have a bar graph and I'd like to do a legend assigning the values of each column to a specific number which I have stored in a row vector. Since the values of the vector change, instead of writing these myself inside legend(), I'd like to be able to provide the row vector as an input.
Assuming a is my row vector, I tried doing this: legend(a)
But it didn't work.
What can I do?
Thanks in advance for your help.
0 Comments
Answers (1)
Vedant
on 12 Mar 2025 at 7:00
To assign the values of a row vector as a legend, each element must be converted into a string. This can be achieved using the “num2str” function. Additionally, setting 'UniformOutput' to false ensures that the output is returned as a cell array rather than a regular array. For further details, refer to the documentation of the commands:
web(fullfile(docroot, "/matlab/ref/arrayfun.html"))
web(fullfile(docroot, "/matlab/ref/num2str.html"))
The following code snippet demonstrates this approach:
a = [10, 20, 30, 40];
legend_labels = arrayfun(@num2str, a, 'UniformOutput', false);
legend(legend_labels);
Below is the output generated using sample data:

0 Comments
See Also
Categories
Find more on Title 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!