How to include labels to a plot?

조회 수: 1 (최근 30일)
Andrei
Andrei 2022년 11월 6일
댓글: Kevin Holly 2022년 11월 6일
So I have some coordinates and I want to include some labels on my plot.
force = 30;
displacement = [10 20 30 40];
cases = ['This is case 1' 'This is case 2' 'This is case 3' 'This is case 4'];
plot (force, displacement, 'x')
for i =1:length(displacement)
text(force, displacement(i), ['\leftarrow ', cases(i)]);
end
I know the problem is with either line 3 or line 6 because cases is saved as char and when I call cases(i) in line 6 it displays only the individual characters instead of everything that is inside the quotation(' ') marks. Does anyone know how to fix this?

채택된 답변

Kevin Holly
Kevin Holly 2022년 11월 6일
편집: Kevin Holly 2022년 11월 6일
force = 30;
displacement = [10 20 30 40];
cases = ["This is case 1" "This is case 2" "This is case 3" "This is case 4"];
plot (force, displacement, 'x')
for i =1:length(displacement)
text(force, displacement(i), ['\leftarrow ', char(cases(i))]);
end
  댓글 수: 2
Andrei
Andrei 2022년 11월 6일
Thank you for your help. Do you mind explaining why it works when
cases = ["This is case 1" "This is case 2" "This is case 3" "This is case 4"];
and not when
cases = ['This is case 1' 'This is case 2' 'This is case 3' 'This is case 4'];
Kevin Holly
Kevin Holly 2022년 11월 6일
Using double quotes creates a string array, where characters enclosed are treated as a single elements.
string_array = "This is case 1";
size(string_array)
ans = 1×2
1 1
Character arrays use single quotes. In this case, each character is treated as a separate element.
character_array = 'This is case 1';
size(character_array)
ans = 1×2
1 14
For more information click here.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by