%% Subjects in a cell array as strings
data = [1 2 3 4 5 6 7 8 9]; % data should be a vector of the data you want to plot
subjects = {'A' 'B' 'C' 'A' 'B' 'C' 'A' 'B' 'C'}; % subject index should be a vector the same size as data which indicates the subject belonging to each element in your data vector
uniqueSubjects = unique(subjects);
cmap = colormap(hsv(length(uniqueSubjects))); % total subjects is the total number of subjects you have
% I used the hsv colormap; use whichever one suits your needs
b = bar(data,1,'FaceColor','flat'); % plot bar graph
for i = 1:length(uniqueSubjects) % for every subject
currentSubject= uniqueSubjects{i}; % get the name of the current subject
indexSubject = strfind(subjects,currentSubject); % find where they are
index = find(not(cellfun('isempty',indexSubject ))); % get their index location
for j = 1:length(index) % go through whole index
b.CData(index(j),:) = cmap(i,:); % set each bar to the color corresponding to current subject
end
end
%% Subject names as numerical array
data = [1 2 3 4 5 6 7 8 9]; % data should be a vector of the data you want to plot
subjects = [1 2 3 1 2 3 1 2 3]; % subject index should be a vector the same size as data which indicates the subject belonging to each element in your data vector
uniqueSubjects = unique(subjects);
cmap = colormap(gray(length(uniqueSubjects ))); % total subjects is the total number of subjects you have
% I used the gray colormap; use whichever one suits your needs
b = bar(data,1,'FaceColor','flat'); % plot bar graph
for i = 1:length(uniqueSubjects) % for every subject
currentSubject= uniqueSubjects(i); % get the name of the current subject
index = find(subjects == currentSubject); % find where they are
for j = 1:length(index) % go through whole index
b.CData(index(j),:) = cmap(i,:); % set each bar to the color corresponding to current subject
end
end




This work by Blake Porter is licensed under a Creative Commons Attribution-Non Commercial-ShareAlike 4.0 International License
Linda
Wonderful , I love color graphs !