How to create legend from cell array

조회 수: 2 (최근 30일)
Farshid Daryabor
Farshid Daryabor 2020년 2월 19일
답변: Yash 2025년 7월 20일
How can I use the list of code number as legend for the Figure. Please find attached the list of number as cell array and Figure.
I want legend for instance as follow;
Platform_01 = 6900805
Platform_02 = 6901831
Platform_03 = 6901832
Platform_04 = 6901895
Platform_05 = 6901900
Platform_06 = 6901961
Platform_07 = 6901962
Platform_08 = 7900590
  댓글 수: 1
Farshid Daryabor
Farshid Daryabor 2020년 2월 19일
It is code to use for plotting
figure(1)
hold on
cellfun(@plot, T_cmems_out, depth_cmems)
set(gca, 'YScale', 'log');
set(gca, 'YTickLabel', get(gca,'YTick'))
grid on
grid minor
set(gca,'FontSize',18);
xlabel('Temperature (^oC)','fontsize',14)
ylabel('Depth (m)','fontsize',14')

댓글을 달려면 로그인하십시오.

답변 (1개)

Yash
Yash 2025년 7월 20일
1. Create a cell array of legend strings that combine "Platform_XX = code".
code_numbers = {6900805, 6901831, 6901832, 6901895, 6901900, 6901961, 6901962, 7900590};
% Build legend strings
legend_str = cell(size(code_numbers));
for k = 1:length(code_numbers)
legend_str{k} = sprintf('Platform_%02d = %d', k, code_numbers{k});
end
2. Pass that cell array to the "legend" function after your plotting commands.
legend(legend_str, 'Location', 'best') % Add the custom legend

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by