How to add a string array on a chart?
5 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Other Formats 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!