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:
