Formatting Legend Entries from a Double in a For Loop

조회 수: 7 (최근 30일)
Camille Woicekowski
Camille Woicekowski 2020년 8월 18일
댓글: Camille Woicekowski 2020년 8월 18일
I'm struggling to make the legend on the graph to display properly.
I want to value of sensor to display with the corresponding line plotted from soil. ( sensor ranges from 12 to 1)
sensor = fliplr(1:1:size(soil,2));
Putting sensor directly into the legend argument gives me the error message "improper type of type of double cannot be used with this function"
I tried
figure();
cc=colorcube(size(soil,2));
j=0;
for i = 1:size(soil,2)
j=j+1;
p=plot(date_soil,soil(:,i), "Color",cc(i,:));
hold on
datetick('x','mmm-dd HH:MM',"keeplimits","keepticks");
strr(j,:) = sprintf('%i',sensor(1,i));
end
hold on
leg=legend(strr);
Which gives me the error "unable to preform assignment because the size of the right side is 1x1 and the size of the left side is 1x2"
I tried different formatSpecs for sprintf and nothing has worked.
How can I get the legend to display the proper values?

채택된 답변

Joel Van Sickel
Joel Van Sickel 2020년 8월 18일
Hello Camille,
I made some assumptions about how soil and date_soil were formatted, but does this script get close to what you are looking for? (it plots the first line white on white so that is not ideal but easily fixed by modifying cc)
soil = [1 2 3 4;2 2 3 4; 3 2 3 4; 4 5 3 4; 5 5 3 4];
date_soil = 1:5;
n = size(soil,2);
sensor = fliplr(1:1:n);
figure();
cc=colorcube(n+1);
strr = cell(n,1);
for i = 1:n
p=plot(date_soil,soil(:,i), "Color",cc(i,:));
hold on
datetick('x','mmm-dd HH:MM',"keeplimits","keepticks");
strr{i,:} = num2str(sensor(i));
end
hold on
leg=legend(strr);
Regards,
Joel

추가 답변 (0개)

카테고리

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