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

조회 수: 15 (최근 30일)
Kathyayani  Nandakumar
Kathyayani Nandakumar 2019년 9월 6일
댓글: Cris LaPierre 2019년 9월 8일
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!!

답변 (1개)

Cris LaPierre
Cris LaPierre 2019년 9월 8일
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
Cris LaPierre
Cris LaPierre 2019년 9월 8일
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')

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by