Help on ploting bar graph using table
81 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2022-5-15
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.
采纳的回答
Star Strider
2022-5-15
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
figure
bar(T{2,:})
set(gca, 'XTick',1:numel(newNames), 'XtickLabel',newNames)
.
8 个评论
dpb
2022-5-16
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.
更多回答(1 个)
dpb
2022-5-16
编辑:dpb
2022-5-16
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...
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!