Plot legend based on variable inside loop
5 views (last 30 days)
Show older comments
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 Comments
Answers (1)
possibility
on 24 Oct 2018
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 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!