Help on ploting bar graph using table

85 views (last 30 days)
Dear Sir/Madam,
Im having difficulty plotting my data in my table to a bar graph. I have renamed my columns of my table and wish to plot a bar graph of the data with the column heading under each bar. Im a beginner in matlab and Ive had a look at the bar help website, but cant seem to solve my issue
Im getting the error: Input arguments must be numeric, datetime, duration or categorical.
I would greatly appreciate help on my problem. Thank You in advance
Best Regards,
Jeevs S
newNames={'Protein A','Protein B','Protein C','Protein D','Protein E','Protein F','Protein G'...
,'Protein H','Protein I','Protein J','Protein K','Protein L''Protein M','Protein N','Protein O','Protein P',...
'Protein Q','Protein R','Protein S','Protein T','Protein U','Protein V'};
T=array2table(data1, 'VariableNames', newNames);
bar(T);
  2 Comments
dpb
dpb on 15 May 2022
What happened to the lesson about categorical variables we created in <help-with-changing-text-in-a-table#answer_963965> just a day or so ago?
>> tSinghBar=readtable('singhDataBar.xlsx');
>> head(tSinghBar)
ans =
2×21 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21
____________________ ____________________ ______ ____ ____ ____ ___________________ ____ ___________________ ____________________ _____ ____________________ ______ _____ _____ _____ ___________________ _____ ___________________ ____________________ _____
1 2 NaN 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0.000110990692337116 0.000194477028347996 0.0001 0 0 0 9.6678862932733e-05 0 0.00026838526259599 0.000239223260270692 0 0.000194477028347996 0.0001 0 0 0 9.6678862932733e-05 0 0.00026838526259599 0.000239223260270692 0
>>
What does any of the above mean? How do you expect to plot whatever this is?
Similar to what we illustrated there, the way/place to put the variable names is in the table if you're going to make a table,
>> tSinghBar.Properties.VariableNames=compose("Protein %c",['A':'U'].')
tSinghBar =
2×21 table
Protein A Protein B Protein C Protein D Protein E Protein F Protein G Protein H Protein I Protein J Protein K Protein L Protein M Protein N Protein O Protein P Protein Q Protein R Protein S Protein T Protein U
____________________ ____________________ _________ _________ _________ _________ ___________________ _________ ___________________ ____________________ _________ ____________________ _________ _________ _________ _________ ___________________ _________ ___________________ ____________________ _________
1 2 NaN 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0.000110990692337116 0.000194477028347996 0.0001 0 0 0 9.6678862932733e-05 0 0.00026838526259599 0.000239223260270692 0 0.000194477028347996 0.0001 0 0 0 9.6678862932733e-05 0 0.00026838526259599 0.000239223260270692 0
>>
although there are only enough columns in the data file for A thru U, not V.
But, the above doesn't seem to be a candidate for a flat table at all -- if one gets out the crystal ball, perhaps the numbers are counts of the second record values???
>> tSinghBar{1,:}.'
ans =
1
2
NaN
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>
Well, no, that doesn't make any sense either, thery're just ordinal numbers with one missing -- we're stumped at what you think these data are, sorry...no idea what to try to do with as is.
gurjeevan singh
gurjeevan singh on 16 May 2022
Dear dpd,
Apologies for the error in the data table. I have updated the data, given by 'data2'
T=readtable('data2.xlsx');
head(T);
Thank you for having a look at my problem
Jeevs S

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 May 2022
There are mismatches between the number of names and the number of variables, and a missing comma between ‘Protein L’ and ‘Protein M’. I am not certain what the first row is for, or how to use it here, since if used with bar3, it dominates the plot .
Anyway, try this —
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/999080/data1.xlsx');
T{:,end+1} = [22; rand*1E-4]; % Add Variable So That Everything Works
newNames={'Protein A','Protein B','Protein C','Protein D','Protein E','Protein F','Protein G'...
,'Protein H','Protein I','Protein J','Protein K','Protein L','Protein M','Protein N','Protein O','Protein P',...
'Protein Q','Protein R','Protein S','Protein T','Protein U','Protein V'};
T.Properties.VariableNames = newNames
T = 2×22 table
Protein A Protein B Protein C Protein D Protein E Protein F Protein G Protein H Protein I Protein J Protein K Protein L Protein M Protein N Protein O Protein P Protein Q Protein R Protein S Protein T Protein U Protein V __________ __________ _________ _________ _________ _________ __________ _________ __________ __________ _________ __________ _________ _________ _________ _________ __________ _________ __________ __________ _________ __________ 1 2 NaN 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 22 0.00011099 0.00019448 0.0001 0 0 0 9.6679e-05 0 0.00026839 0.00023922 0 0.00019448 0.0001 0 0 0 9.6679e-05 0 0.00026839 0.00023922 0 1.9708e-05
figure
bar(T{2,:})
set(gca, 'XTick',1:numel(newNames), 'XtickLabel',newNames)
.
  8 Comments
gurjeevan singh
gurjeevan singh on 16 May 2022
Thank you for the feedback. The data above is part of a larger dataset and converting to columns would not be ideal for the data given. However, I most definetly agree with your points above. Thank you for helping with my problem and giving more insight into Matlab coding
dpb
dpb on 16 May 2022
Attach a representative section of the dataset...there still may be "more better" ways to import/use it...we can only see through a tiny peephole here.

Sign in to comment.

More Answers (1)

dpb
dpb on 16 May 2022
Edited: dpb on 16 May 2022
Pursuant to the previous comments; my suggestions would result in something like
>> tSinghBar=readtable('data2.xlsx');
>> tSinghBar.Protein=categorical(tSinghBar.Protein)
tSinghBar =
20×2 table
Protein Measure1
_______ __________
A 0.00011099
B 0.00019448
C 0.0001
D 0
E 0
F 0
G 9.6679e-05
H 0
I 0.00026839
J 0.00023922
K 0
L 0.00019448
M 0.0001
N 0
O 0
P 0
Q 9.6679e-05
R 0
S 0.00026839
T 0.00023922
>> bar(tSinghBar.Protein,tSinghBar.Measure1)
>>
I attached the updated/reformatted Excel file for your convenience/viewing pleasure...

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!