Formatting Legend Entries from a Double in a For Loop

4 views (last 30 days)
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
for i = 1:size(soil,2)
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));
hold on
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?

Accepted Answer

Joel Van Sickel
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);
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));
hold on

More Answers (0)


Community Treasure Hunt

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

Start Hunting!