Formatting Legend Entries from a Double in a For Loop
6 views (last 30 days)
Show older comments
Camille Woicekowski
on 18 Aug 2020
Commented: Camille Woicekowski
on 18 Aug 2020
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 Comments
Accepted Answer
Joel Van Sickel
on 18 Aug 2020
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
More Answers (0)
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!