フィルターのクリア

Create Legend From Array

272 ビュー (過去 30 日間)
Christopher Saltonstall
Christopher Saltonstall 2017 年 12 月 19 日
編集済み: KL 2017 年 12 月 19 日
Hello,
I have an array
nN = 6;
N = 1:nN;
I want to make a legend where nN changes and so may not be known ahead of time. I found the following solution on another post, but it doesn't work for me.
legendCell = cellstr(num2str(dope, 'N=%-d'));
legend(legendCell)
I get the error.
Error using legend>process_inputs (line 563)
Cell array argument must be a cell array of character vectors.
Error in legend>make_legend (line 328)
[autoupdate,orient,location,position,children,listen,strings,propargs]
= process_inputs(ha,argin); %#ok
Error in legend (line 282)
make_legend(ha,args(arg:end),version);
So, I tried
for iN = 1:nN
legendCell{iN} = cellstr(['n = ' num2str(N(iN))]);
end
legend(legendCell)
which gives a similar error. However, the following does work.
legendCell = {'n=1', 'n=2', 'n=3', 'n=4', 'n=5', 'n=6'};
legend(legendCell)
Why does the later work but the others don't?

採用された回答

Jos (10584)
Jos (10584) 2017 年 12 月 19 日
In the first two codes, legendCell is a cell array of cells ( = {{..} {...}} ) rather than a cell array of strings (= {'..' '..'})! You have a pair of curly braces too many, as cellstr creates a cell, which you then put into a cell :)
Try this:
for iN = 1:nN
legendCell{iN} = num2str(N(iN),'N=%-d');
end

その他の回答 (1 件)

KL
KL 2017 年 12 月 19 日
編集済み: KL 2017 年 12 月 19 日
Without a loop maybe,
legendCell = strcat('N=',string(num2cell(1:nN)))
and then as you might know,
legend(legendCell)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by