Clear Filters
Clear Filters

How can I do the x-axis labeling with non-numeric values ​​in a bar chart in Excel?

3 views (last 30 days)
Everthing is working fine. But I want to label the bars at the x-axis with Non-Numeric values. For Example the first bar has the name "Dog", the second one has the name "cat" and so on. What ist the function for that?
Excel = actxserver('Excel.Application');
Workbook = Excel.workbooks.Open(ActualFilePath);
% Add a chart to the workbook
Chart = invoke(Workbook.Charts,'Add');
% Specify the range for the chart
RangeColumnChart= "Sheet1!$B$" +"2" + ":$C$" + string(RowOfFilteredData)
invoke(Chart, 'SetSourceData', Excel.Range(RangeColumnChart));
%Chart.XValues = [TableFilteredData{:,2}]; Chart.Values = [TableFilteredData{:,3}];
Axes = invoke(Chart,'Axes',1); set(Axes,'HasTitle',1); set(Axes.AxisTitle,'Caption','NameOfXAxis');
Axes = invoke(Excel.ActiveChart,'Axes',2); set(Axes,'HasTitle',1); set(Axes.AxisTitle,'Caption','NameOfYAxis');
Excel.ActiveChart.HasTitle = 1; Excel.ActiveChart.ChartTitle.Characters.Text = 'TitleOfColumnChart';
% Make excel visible
Excel.Visible = true;
Quit(Excel)

Answers (1)

Amish
Amish on 12 Feb 2024
Hi Marco,
I see that you are trying to control Excel from MATLAB and configure the chart's axes, titles and main title. As I can understand, you want to label the bars at the x-axis with Non-Numeric values. Since, you have mentioned about the existing code snippet working as expected, I asusme that you already have a ActiveX connection to Excel.
Now to label the bars at the x-axis, you will first need to make sure that you have the non-numeric labels in a cell range in Excel. You can then use the "SetSourceData" mehtod to set the range that contains the category labels (non-numeric values). In Excel, the categories for the x-axis are determined by the range of cells that you specify for the labels when creating the chart. Therefore, the modified version of your given code would look like this:
Excel = actxserver('Excel.Application');
Workbook = Excel.workbooks.Open(ActualFilePath);
Chart = invoke(Workbook.Charts,'Add');
RangeColumnChart = "Sheet1!$B$2:$C$" + string(RowOfFilteredData);
invoke(Chart, 'SetSourceData', Excel.Range(RangeColumnChart));
% Code for ading the non-numeric labels on x-axis :
% Assuming the non-numeric labels are in the range "Sheet1!$A$2:$A$(n+1)"
LabelsRange = "Sheet1!$A$2:$A$" + string(RowOfFilteredData);
% Set the category labels for the x-axis
Chart.SetSourceData(Excel.Range(RangeColumnChart));
Chart.Axes(1).CategoryNames = Excel.Range(LabelsRange).Value;
Axes = invoke(Chart,'Axes',1);
set(Axes,'HasTitle',1);
set(Axes.AxisTitle,'Caption','NameOfXAxis');
Axes = invoke(Excel.ActiveChart,'Axes',2);
set(Axes,'HasTitle',1);
set(Axes.AxisTitle,'Caption','NameOfYAxis');
Excel.ActiveChart.HasTitle = 1;
Excel.ActiveChart.ChartTitle.Characters.Text = 'TitleOfColumnChart';
% Make Excel visible
Excel.Visible = true;
Quit(Excel);
The "Chart.Axes(1).CategoryNames" line is the one that actually assigns the non-numeric values to the x-axis of the chart, where "Axes(1)" refers to the x-axis (category axis) of the chart.
Also, remember to save any changes in Excel before closing the application as you are using the "Quit(Excel)" command.
You can also refer the this dicussion for more details on using charts in Excel using MATLAB:
Hope this helps!

Tags

Community Treasure Hunt

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

Start Hunting!