![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/979670/Screenshot_20220427-002644_Samsung%20Internet.jpg)
How to add text to a line in the plot?
조회 수: 246 (최근 30일)
이전 댓글 표시
Hi all,
This code:
load('z_means'); load ('z_diffs'); load ('z_CR_ofmean')
figure ('color','w');
plot(z_means,z_diffs,'sr', 'MarkerSize', 9)
hold on
%Plot lines
plot(z_means,zeros(1,length(z_means)),'r', 'LineWidth', 0.7); %%%plot zero
plot(z_means, ones(1,length(z_means)).*z_CR_ofmean(1),'r--', 'LineWidth', 0.6); %%%plot the upper CR
plot(z_means, ones(1,length(z_means)).*z_CR_ofmean(2),'r--', 'LineWidth', 0.6); %%%plot the lower CR
% Add text
text(z_CR_ofmean, [mynum2str(z_CR_ofmean(1)) ''],'HorizontalAlignment','left','VerticalAlignment','middle','fontsize',6);
text(z_means, [mynum2str(z_means,2) ''],'HorizontalAlignment','left','VerticalAlignment','middle','fontsize',6);
text(z_CR_ofmean, [mynum2str(z_CR_ofmean(2))''],'HorizontalAlignment','left','VerticalAlignment','middle','fontsize',6);
Produces this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/979535/image.jpeg)
and the error:
Error using text
First two or three arguments must be numeric doubles.
Wheareas I want:
- Add text to each line correctly (can be any text as an example). Can you help please?
댓글 수: 0
채택된 답변
Simon Chan
2022년 4월 26일
By using this function, you can also add the text in the following example.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/979670/Screenshot_20220427-002644_Samsung%20Internet.jpg)
댓글 수: 0
추가 답변 (1개)
Bjorn Gustavsson
2022년 4월 26일
With text you specify both the x and y-coordinate of the text. For example:
th = text(14,-0.5,'text-test that will start at x: 14, y: -0.5');
So you just need to specify an/the x-location before the specification of the y-location (z_CR_ofmean).
HTH
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!