
Provide a row vector as legend
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Vedant Shah
2025년 3월 12일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!