How to label names in bar graph?

49 Ansichten (letzte 30 Tage)
OMAR MOUSA
OMAR MOUSA am 29 Okt. 2023
Kommentiert: the cyclist am 30 Okt. 2023
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

Akzeptierte Antwort

Star Strider
Star Strider am 29 Okt. 2023
Bearbeitet: Star Strider am 29 Okt. 2023
One approach (using xticklabels introduced in R2016b, as was string) —
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 Kommentare
Star Strider
Star Strider am 30 Okt. 2023
As always, my pleasure!
the cyclist
the cyclist am 30 Okt. 2023
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()].

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

the cyclist
the cyclist am 29 Okt. 2023
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.

Community Treasure Hunt

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

Start Hunting!

Translated by