Clear Filters
Clear Filters

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)
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

Accepted Answer

Star Strider
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'})
Table1 = 10×3 table
Fe Co Al ________ _________ ________ 0.028607 0.74532 0.76599 0.069023 0.48121 0.96832 0.76701 0.82539 0.21736 0.21192 0.30695 0.60302 0.013086 0.79411 0.069884 0.40869 0.97362 0.58056 0.049978 0.097162 0.50264 0.16866 0.65555 0.1194 0.58151 0.9482 0.32777 0.50073 0.0056197 0.4368
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.
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!