Plot legend based on variable inside loop
7 次查看(过去 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!