Why am I getting "Error using bar (line 127) Input arguments must be numeric, datetime, duration or categorical"?

68 ビュー (過去 30 日間)
I am trying to create a stacked bar chart of revenue data by fiscal quarter using data from a table. The data I want to plot is in columns 2-6, while the quarter names (string) are in column 1.
Here is a cleaned version of the code:
clear
clc
close all
load table_data.mat
bar(quarterly_revenue(:,2:end),'stacked')
legend(quarterly_revenue.Properties.VariableNames(2:end))
title('Product Revenue Over Time')
x = 1:height(quarterly_revenue);
xticks(x)
xticklabels(quarterly_revenue(:,1))
ytickformat('usd')
When I run the code, I get an error:
Error using bar (line 127)
Input arguments must be numeric, datetime, duration or categorical.
Error in example_error_using_bar (line 5)
bar(quarterly_revenue(:,2:end),'stacked')

採用された回答

Pat Canny
Pat Canny 2019 年 1 月 30 日
編集済み: Pat Canny 2019 年 1 月 30 日
You are using parentheses where you should be using curly braces. To access the content of the table, use curly braces.
Here's a quick demonstration. Type this in the Command Window after loading the table data:
quarterly_revenue(:,2:end)
This will return a table (look at the "ans" variable in the Workspace). The "bar" command doesn't recognize tables as a valid input.
Instead, try this:
quarterly_revenue{:,2:end}
This will return an array of doubles (numeric). The "bar" command knows what to do with these.
You are also using parentheses elsewhere in your code where a curly brace should be used. You must be in marketing!
Here's the correct implementation:
clear
clc
close all
load table_data.mat
bar(quarterly_revenue{:,2:end},'stacked')
legend(quarterly_revenue.Properties.VariableNames{2:end})
title('Product Revenue Over Time')
x = 1:height(quarterly_revenue);
xticks(x)
xticklabels(quarterly_revenue{:,1}) % Each bar has a label of the fiscal quarter's name
ytickformat('usd')
Enjoy that sweet, sweet bar chart!

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by