Not enough input arguments when using bar()

118 ビュー (過去 30 日間)
Raghav Rathi
Raghav Rathi 2023 年 9 月 20 日
コメント済み: Star Strider 2023 年 9 月 20 日
Hi,
I am trying to generate a bar graph specifically 'Specify Bar Locations as String Vector' from the documentation.
x = ["Outdoor1" "Outdoor2" "Outdoor3" "Outdoor4"];
y = [540 524 515 424];
>> bar(x,y)
Error using bar
Not enough input arguments.
I am not able to figure out why am I getting the above error when the example mentioned in the documentation is pretty much the same but seems to be working.
  2 件のコメント
Mario Malic
Mario Malic 2023 年 9 月 20 日
Maybe you have a function called bar in your current folder?
which bar
/MATLAB/toolbox/matlab/specgraph/bar.m
Raghav Rathi
Raghav Rathi 2023 年 9 月 20 日
/usr/local/MATLAB/R2023a/toolbox/matlab/specgraph/bar.m
There is no other function named bar in the PWD

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2023 年 9 月 20 日
Use the categorical function —
x = categorical(["Outdoor1" "Outdoor2" "Outdoor3" "Outdoor4"]);
y = [540 524 515 424];
figure
bar(x,y)
Another option is to use numerical values, for example 1:4 and then use ‘x’ as tick labels —
x = 1:4;
xl = ["Outdoor1" "Outdoor2" "Outdoor3" "Outdoor4"];
y = [540 524 515 424];
figure
bar(x,y)
set(gca,'XTickLabel',xl)
It all depends on what you want to do.
.
  2 件のコメント
Raghav Rathi
Raghav Rathi 2023 年 9 月 20 日
Thanks for the help.
Star Strider
Star Strider 2023 年 9 月 20 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2023 年 9 月 20 日
編集済み: Dyuman Joshi 2023 年 9 月 20 日
It works fine here - R2023b but does not on my MATLAB app - R2021a.
Since you have R2023a, it means that the support for string array as input to bar must have been implemented in R2023b.
Workaround > Convert x to a categorical array and then plot -
x = ["Outdoor1" "Outdoor2" "Outdoor3" "Outdoor4"];
x = categorical(x);
y = [540 524 515 424];
bar(x,y)
  1 件のコメント
Raghav Rathi
Raghav Rathi 2023 年 9 月 20 日
Its wotking now, Thanks

サインインしてコメントする。

カテゴリ

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

タグ

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by