How to label points on a plot based on the data in table (not based on the number of points)?

Hi! I have a question regarding labelling points in matlab. I have a figure which needs to be labelled based on the data in the table. I read the example of labelling, but it mostly label based on the number of the points, not the data itself. I attach the picture of the figure and their corresponding label. The 'sampling point' should be labelled based on the data in LabelSamplPoint in cell 16 (I attach the picture of the table). I am confused about how to put the data in cell 16 as the label to the points in the plot. The point itself is from cell 15 for y-axis and the x-axis is cell 1. Could you please help me? Thank you very much for your help.

 Réponse acceptée

mask = ~isnan(finalCSVnew.LabelSamplPoint);
xvals = finalCSVnew{mask, 1}; %assuming you are right
yvals = finalCSVnew{mask, 15}; %are you sure?? This appears to be SamplError not a y value
labvals = FinalCSVnew.LabelSamplPoint(mask);
text(xvals, yvals, labvals)

3 commentaires

Sorry, it should be cell 13. I tried your code above, but I got an error
"Error using text
Character vector argument expected after 2 or 3 numeric arguments"
I integrate your code above to my code for plotting the figure :
%%Plot Flow [m3], Water Level [m], and Sampling Point
% define x-axis and y-axis
x=finalCSVnew{:,'DateAndTime'};
y1=finalCSVnew{:,'Durchflusslm'};
y2=finalCSVnew{:,'WaterLevel'};
y3=finalCSVnew{:,'SamplPoint'};
y4=finalCSVnew{:,'SamplChange'};
y5=finalCSVnew{:,'SamplError'};
% plot graphs with two y-axis, left side for Flow and
% right side for water level
figure(1);
yyaxis left;
plot(x,y1);
title('Flow, Water Level and Sampling Point');
xlabel('Date and Time');
ylabel('Flow [m3/h]');
% plotting y3, y4, and y5 in figure 1 by using 'o' character
hold on
plot(x,y3,'o','MarkerEdgeColor','k') % use black as marker edge color
hold on
plot(x,y4,'o','MarkerEdgeColor','m','MarkerFaceColor','m') % use magenta as marker edge color and marker face color
hold on
plot(x,y5,'o','MarkerEdgeColor','r','MarkerFaceColor','r') % use red as marker edge color and marker face color
yyaxis right;
plot(x,y2);
ylabel('Water Level [m]');
grid on % show grid on plot
datacursormode on % enable to display data value interactively in the plot
legend('Flow','Sampling point','Sampling bottle change','Sampling error','Water level','Location','Northeast','Orientation','Vertical')
clearvars x y1 y2 y3 y4 y5 % clear temporary variables
mask = ~isnan(finalCSVnew.LabelSamplPoint);
xvals = finalCSVnew{mask, 1};
yvals = finalCSVnew{mask, 13};
labvals = finalCSVnew.LabelSamplPoint(mask);
text(xvals, yvals, labvals)
Do you think I should integrate something for x and y so that the label can be read in the figure? Thank you so much for your help, Walter
labvals = sprintfc('%g', finalCSVnew.LabelSamplPoint(mask));

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by