How to add a string array on a chart?

6 views (last 30 days)
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!

Accepted Answer

Ameer Hamza
Ameer Hamza on 18 May 2020
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 Comments
hmhuang
hmhuang on 18 May 2020
Thanks! You solved my problem.
Ameer Hamza
Ameer Hamza on 18 May 2020
I am glad to be of help!

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!