Formatting Legend Entries from a Double in a For Loop
3 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Legend 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!