Plot legend based on variable inside loop

조회 수: 7 (최근 30일)
Kai Åsvik
Kai Åsvik 2018년 7월 22일
답변: possibility 2018년 10월 24일
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);

답변 (1개)

possibility
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.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by