Unable to Add Legend to Plot

조회 수: 17 (최근 30일)
Kylen
Kylen 2024년 6월 14일
댓글: Voss 2024년 6월 14일
Hello All,
I'm writing a script to do some data processing and plotting. It came time to add a legend to one of my plots, but Matlab started throwing me errors. In frustration, I thought I was forgetting how to do something that I've done scores of times, so I opened up the Matlab documentation on Legends (https://www.mathworks.com/help/matlab/creating_plots/add-legend-to-graph.html). Even copy and pasting the example on the Help page (see below) results in an error.
figure
x1 = linspace(0,5);
y1 = sin(x1/2);
plot(x1,y1)
hold on
x2 = [0 1 2 3 4 5];
y2 = [0.2 0.3 0.6 1 0.7 0.6];
scatter(x2,y2,'filled')
hold off
legend('sin(x/2)','2016')
" Execution of script legend as a function is not supported:\\filepath\filepathxxxx
Error in legendError (line 20)
legend('sin(x/2)','2016') "
Anybody have an idea? Did scomething corrupt my version of Matlab? I'm running 2023b and haven't had any such issues before.

채택된 답변

Voss
Voss 2024년 6월 14일
Looks like you have a script called "legend.m" (located in "\\filepath\filepathxxxx") which is conflicting with running the built-in "legend.m". You should rename that script or remove its folder from the path.
  댓글 수: 2
Kylen
Kylen 2024년 6월 14일
That was the problem for my troubleshooting script. My main code still kept erroring out. Closed and re-opened Matlab a few times with no luck. Finally restarted my computer and it worked again.
I had an error in my main code and it turns out I had a totally different error (due to my poor script naming), and both are now resolved.
Thank you.
Voss
Voss 2024년 6월 14일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by