bar chart allowing for x values to sometimes be the same

Hello. I am plotting the data from a UITable using a bar chart
I am plotting color versus TotalArea, and quite often the color column has the same value e.g 457. This causes the bar chart to crash, so I have to manually go in and append _1, _2 etc..
Is there a way to get the barchart working without having to manually intervene. Please note, I don't always have the same X values (Color), sometimes several values with repeats, sometimes all different values. The values will be either 405, 457 or 532. For any repeats, I still want to plot them as seperate bars
Here is my current code.
cla(app.UIAxes2,'reset');
app.UIAxes2.NextPlot = 'replacechildren'; %Need this in 2020
data=app.UITable.Data;
data= flipud(data); %Most recent data is at the top
%Create bar chart
Y=cell2mat(data(:,4))
X=data(:,2);
Xdata = categorical(X);
X = reordercats(Xdata,X);
b1=bar(X,Y,'Parent',app.UIAxes2);
Thanks
Jason

 Accepted Answer

This is the expected behavior when you plot specifying x and y. However, I can think of a simple work around.
You xtick location and xticklabel are separate properties. You can take advantage of this to plot your values according to their index then change the label to be the color.
% Create table
TotalArea = 1e7*rand(5,1);
Color = [2 5 8 10 5]';
data = table(Color,TotalArea);
% bar plot - specify label
bar(data.TotalArea)
xticklabels(data.Color)

5 Comments

when I use th data from UITable i get the error message:
Dot indexing is not supported for variables of this type.
You would need to adapt it slightly for a uitable in an app. Here's an example that will run in your desktop.
% Create uitable
TotalArea = 1e7*rand(5,1);
Color = [2 5 8 10 5]';
d = table(Color,TotalArea);
fig = uifigure;
uit = uitable(fig,'Data',d);
% bar plot - specify label
bar(uit.Data.TotalArea)
xticklabels(uit.Data.Color)
Hi, So I had this - Isn't it the same?
data=app.UITable.Data;
b1=bar(data.TotalArea,'Parent',app.UIAxes2); %b1=bar(X,Y,'Parent',app.UIAxes2);
xticklabels(data.Color)
Close. In an app, any code that modifies an axes needs to have the axes handle included.
data=app.UITable.Data;
bar(data.TotalArea,'Parent',app.UIAxes); %b1=bar(X,Y,'Parent',app.UIAxes2);
xticklabels(app.UIAxes,data.Color)

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Tags

Asked:

on 18 Dec 2020

Commented:

on 18 Dec 2020

Community Treasure Hunt

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

Start Hunting!