How to add annotations to a plot by accessing data from a table?

15 Ansichten (letzte 30 Tage)
Kathyayani  Nandakumar
Kathyayani Nandakumar am 6 Sep. 2019
Kommentiert: Cris LaPierre am 8 Sep. 2019
I have a table with a combination of strings and numbers as follows:
A 0 1
B 5 0
C 0 1
D 0 0
E 3 4
This table is used to add annotations to a plot. The idea is to display rows with non zero values in it, i.e., in plot 1, I need to display B-5, E-3 and in plot 2, A-1, C-1, E-4, as shown in the image below.
Can someone suggest a way for doing this in matlab? Thanks in advance!!

Antworten (1)

Cris LaPierre
Cris LaPierre am 8 Sep. 2019
Use the text function.
You can use sprintf to build the text to display on the plot.
If you need it to be automated, use logical indexing to identify which rows contain non-zero entries. Here's a quick attempt:
% create the initial plots
theta = 0:0.1:2*pi;
siny = sin(theta);
cosy = cos(theta);
subplot(1,2,1)
plot(theta, siny)
subplot(1,2,2)
plot(theta,cosy)
% Now add text
for p = 2:width(dispTxt)
idx = dispTxt{:,p} > 0;
txt = sprintf("%s-%s\n",[dispTxt{idx,1},dispTxt{idx,p}]');
% Select desired subplot
subplot(1,2,p-1)
xlim([0 8])
text(7,-0.5,txt)
end
Of course, you could also add the text at the same time you are creating the plots.
  1 Kommentar
Cris LaPierre
Cris LaPierre am 8 Sep. 2019
BTW, dispTxt is a table containing the values you showed in your question.
T = ["A" "B" "C" "D" "E"];
V1 = [0 5 0 0 3];
V2 = [1 0 1 0 4];
dispTxt = table(T',V1',V2')

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by