Plot legend based on variable inside loop
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
possibility
el 24 de Oct. de 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 comentarios
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!