Plot legend based on variable inside loop
8 ビュー (過去 30 日間)
古いコメントを表示
I've seen several answers to this question, but none appear to work. The following example taken from another question, when pasted directly, gives this error message:
Error using cellstr (line 44) Element 1 is not a string scalar or character array. All elements of cell input must be string scalars or character arrays.
Error in legend>process_inputs (line 547) strings = cellstr(argin{n});
Error in legend>make_legend (line 306) [autoupdate,orient,location,position,children,listen,strings,propargs] = process_inputs(ha,argin); %#ok
Error in legend (line 259) make_legend(ha,args(arg:end),version);
Error using cellstr (line 44) Element 6 is not a string scalar or character array. All elements of cell input must be string scalars or character arrays.
Error in legend>process_inputs (line 547) strings = cellstr(argin{n});
Error in legend>make_legend (line 306) [autoupdate,orient,location,position,children,listen,strings,propargs] = process_inputs(ha,argin); %#ok
Error in legend (line 259) make_legend(ha,args(arg:end),version);
Error in Untitled (line 9) legend(lgd);:
time = linspace(0, 2*pi); % Create Data
beta = 1:5; % Create Data
y = sin(time(:)*beta); % Create Data
for beta_val = beta
plot(time, y(:,beta_val));
hold on
lgd{beta_val} = sprintf('beta = %0.0f', beta(beta_val));
end
legend(lgd);
0 件のコメント
回答 (1 件)
possibility
2018 年 10 月 24 日
I had the same error. When I search, I reach your topic. Your code worked well on my MATLAB version; which is R2017b. I don't know if you're still having the problem. If you are,
- try editing the last line as
legend(string(lgd));
- be sure your whole script is not within a function as cell array lgd won't be held within the function.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!