フィルターのクリア

Matlab Legend Function dose not work at all

26 ビュー (過去 30 日間)
Zachery
Zachery 2024 年 4 月 3 日 17:31
回答済み: Voss 2024 年 4 月 3 日 18:26
I am simply trying to make legends on a graph with multiple traces on a single graph which should be easy with the 'legend' function.
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
legend('legend1','legend2')
When I run the code above I get error message "Index in position 1 exceeds array bounds (must not exceed 1)." on the line for the legend I am using version MatlabR2021a if that helps

採用された回答

Star Strider
Star Strider 2024 年 4 月 3 日 18:10
編集済み: Star Strider 2024 年 4 月 3 日 18:13
There are no errors when I run your code —
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
legend('legend1','legend2')
If the problem is only with the legend call, check to see if you have a variable or function called ‘legend’ somehwere in your workspace or on your MATLAB search path —
which legend -all
/MATLAB/toolbox/matlab/scribe/legend.m
The result should be only what is shown here. If there are any other entries, that points to the problem. The soution is to re-name your variable to something different, and that makes sense in the context of your code.
.
EDIT — Corrected typographical errors.
.

その他の回答 (1 件)

Voss
Voss 2024 年 4 月 3 日 18:26
You have a variable called legend in your workspace interfering with you calling the legend function.
Run
clear legend
to clear that variable and then run your code again.

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by