フィルターのクリア

How to add a string array on a chart?

5 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by