I am holding onto a scatter plot and adding data to it through a for loop. For each data set, I would like to label it in the legend but I am running into some problems.
50 views (last 30 days)
Show older comments
Luisana Rodriguez Sequeira
on 25 Jun 2021
Commented: Star Strider
on 28 Jun 2021
Below is the line of code I am using. As you can see I am running a for loop where each time it adds a different data set to a scatter plot (this part is working beautifully), but I can't seem to figure out how to change the display name for each data added--obviously this doesn't work and I feel like the j would be useful, but as my comment suggests, it hasn't been so far.
for j=1:height(Fe4plsr),
figure(1)
hold on
B=table2array(A4plsr(j, 2:209));
scatter(A, B, "DisplayName", "Fe")
%cant add j outside of "" to know which, cant add j inside "" because it wouldn't change
xlabel('Wavelengths(nm)')
ylabel('Frequency')
A4corr(j, :)= [B];
end
0 Comments
Accepted Answer
Star Strider
on 25 Jun 2021
I have no idea what the matrices are or what the different display names would be.
One approach would be to get the variable names of the table you want to plot (I have no idea how to eimulate that with your code without your data) and then use that as the display name
Table1 = array2table(rand(10,3), 'VariableNames',{'Fe','Co','Al'})
VN = Table1.Properties.VariableNames;
figure(1)
hold on
for j=1:3,
scatter((1:10), Table1{:,j}, "DisplayName", VN{j})
end
hold off
legend('Location','best')
Another option of course is to simply use the ‘VN’ cell array in this example as a legend argument.
.
2 Comments
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!