How to label names in bar graph?
52 ビュー (過去 30 日間)
古いコメントを表示
I faced a problem in labeling names which gave me errors all the time. the labels include one name and several numbers. the code runs only with number labels but the name cannot be included in the labels
I wrote the code down and got the figure how can I fix this and change the number 1 with "THD" ?
I used categorical, but the order of labels has showed randomly

dataA = {DBTGA, DBT1A, DBML1A, DBML2A, CPACA, EMSB1A, SSBMFA, AggA};
dataB = {DBTGB, DBT1B, DBML1B, DBML2B, CPACB, EMSB1B, SSBMFB, AggB};
dataC = {DBTGC, DBT1C, DBML1C, DBML2C, CPACC, EMSB1C, SSBMFC, AggC};
titles = {'DB TG','DB T1','DB ML 1','DB ML 2','CP AC','EMSB 1','SSB MF','Aggregate'};
xlables = ["THD" 3 5 7 9 11 13 15]; % when i put 1 rather THD it works but for "THD" doesn't work
for i = 1:numel(dataA)
dataA{i}=mean(dataA{i});
dataB{i}=mean(dataB{i});
dataC{i}=mean(dataC{i});
end
for i = 1:numel(dataA)
subplot(3,3,i)
bar(xlables,[dataA{i};dataB{i};dataC{i}].')
xlabel({'Harmonic Order'},'FontSize', 10)
ylabel({'% of fundimental'},'FontSize', 10)
title(titles{i},'FontSize', 10)
grid, grid minor
end
0 件のコメント
採用された回答
Star Strider
2023 年 10 月 29 日
編集済み: Star Strider
2023 年 10 月 29 日
Data = rand(8,3);
xlables = ["THD" 3 5 7 9 11 13 15]; % when i put 1 rather THD it works but for "THD" doesn't work
figure
bar(Data)
xticklabels(xlables)
EDIT — (29 Oct 2023 at 21:50)
Added reference to string and xticklabels being introduced in the same release. Content otherwise unchanged.
.
3 件のコメント
the cyclist
2023 年 10 月 30 日
In newer versions of MATLAB, you can do the simpler
Data = rand(8,3);
xlables = ["THD" 3 5 7 9 11 13 15];
bar(xlables,Data)
Note that part of the reason this works is that MATLAB converts the numeric values in xlables into strings, so the variable is a string array [which can be used directly in bar()].
その他の回答 (1 件)
the cyclist
2023 年 10 月 29 日
Can you upload the data, so that we can run your code? You can use the paper clip icon in the INSERT section of the toolbar.
What version of MATLAB are you using? Certain data types cannot be used as x-axis values in older releases.
I expect this will work if you use this version of the syntax for bar:
bar([dataA{i};dataB{i};dataC{i}].') % Using the default x-axis values
set(gca,"XTickLabels",xlables)
This method uses your vector only to label the ticks, not as the values of the ticks.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!