How can I generate changing length of legend for a scatter graph generated in a while loop?

1 次查看(过去 30 天)
Hi everyone,
So i have the following code. where I would like to add legend for the scatter graph in the while loop. this is only a segment of the code. but basically the number of coloumns of M, M_2, M_3 is changing according to the user prompt. I want to add legend to the generated scatter plot, that varies in length ? (the variable f represents the user input)
M = [ 0.12 0.21 0.356 0.458 0.3659 0.2458 0.5412 0.324 0.2121];
M_2 = [0.35 0.56 0.12 0.321 0.987 0.88 0.44 0.66 0.23];
M_3 = [0.11 0.22 0.87 0.45 0.124 0.648 0.98 0.2 0.3];
cc = [-2 -1 0];
f = 7;
s = 17 - f;
while( s > 1)
s = s-1;
C = [M(s) M_2(s) M_3(s)];
hold on
scatter(cc,C);
hold on
end
h = lsline;
for k = 1:numel(h)
B = polyfit(h(k).XData, h(k).YData,1);
slope(k) = B(1);
intercept(k) = B(2);
end
Thank you for your help!

回答(1 个)

Bhaskar R
Bhaskar R 2019-11-12
M = [ 0.12 0.21 0.356 0.458 0.3659 0.2458 0.5412 0.324 0.2121];
M_2 = [0.35 0.56 0.12 0.321 0.987 0.88 0.44 0.66 0.23];
M_3 = [0.11 0.22 0.87 0.45 0.124 0.648 0.98 0.2 0.3];
cc = [-2 -1 0];
f = 7;
s = 17 - f;
while( s > 1)
s = s-1;
C = [M(s) M_2(s) M_3(s)];
hold on
scatter(cc,C);
hold on
end
legend('show'); % add legend command
h = lsline;
for k = 1:numel(h)
B = polyfit(h(k).XData, h(k).YData,1);
slope(k) = B(1);
intercept(k) = B(2);
end

类别

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