How to add a string array on a chart?

조회 수: 4 (최근 30일)
hmhuang
hmhuang 2020년 5월 18일
댓글: Ameer Hamza 2020년 5월 18일
I would like to add 3 lines of text stored in a cell array to my chart, but failed to display it onto the chart.
MATLAB complained that "Cell array can contain only non-empty character vectors, string vectors, or numbers."
The implementation is like:
% point is a 1 x 6 string array
point = "4^1+1" "4^2+1" "4^3+1" "4^4+1" "4^5+1" "4^6+1"
txt = { sprintf('U0 = %.1f', U0), sprintf('Gamma = %.1f', gamma), ...
["Grid points = " point] };
xlim = get(gca,'xlim');
ylim = get(gca,'ylim');
% Add text to chart
text(xlim(1)*2, ylim(2)*0.01, txt, 'FontSize', 16);
Result:
Error using text
Cell array can contain only non-empty character vectors, string vectors, or numbers.
Error in assignment2 (line 76)
text(xlim(1)*2, ylim(2)*0.01, txt, 'FontSize', 16);
The content of 'txt' is:
txt =
1×3 cell array
{'U0 = -10.0'} {'Gamma = 1.0'} {1×6 string}
My question is, how to add a string array onto the chart?
or how to convert a string array to a string vector so that it satisfied the requirements of cell array?
Thanks in advance!

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 18일
Try this
point = "Grid points = " + sprintf('4^%d ', 1:6);
txt = { sprintf('U0 = %.1f', 1), sprintf('Gamma = %.1f', 2), ...
point};
xlim = get(gca,'xlim');
ylim = get(gca,'ylim');
% Add text to chart
text(xlim(1)*2, ylim(2)*0.01, txt, 'FontSize', 16);
  댓글 수: 2
hmhuang
hmhuang 2020년 5월 18일
Thanks! You solved my problem.
Ameer Hamza
Ameer Hamza 2020년 5월 18일
I am glad to be of help!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by