Why is the colormap value of my bar plot returning an error
1 view (last 30 days)
Show older comments
Hello, me and my peers are trying to create a program that would calculate for the percentage of students present in class per day, and plot that percentage on a bar graph. I used the following codes to determine each individual bar's color value based on the min and max y values.
P.S. CP == array of percentage attendnace per day example: [100 60 50] first day = 100% present, second day = 60% present, third day = 50% present
To test it, Enter incremental Day # for each run. Example Day #1 for the first run. Then Day #2 for the second
Then assign boolean values of 1 to all 10 students to signify that they are present
If 100% of the class is present on the 1st index and on the very last index, the program returns an error:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in attendancechecker (line 109)
set(h, 'FaceColor', map(ind,:)) ;
The program comes to an error if both min_y and max_y are equal.
Can anyone tell me how to overcome this? The full code is listed below.
% Initializing variables
Is115(:,:) = (["Calil" "Clyde" "Erickuh" "Mclane" "Julian" "Antoinne" "Aerielle" "Reece" "Sir Nohay" "Julian"])';
disp("Is the student present or not?")
%Assigning boolean values of attendance to the 7 days of a week
D = input("DAY #");
for ii = 1:length(Is115)
fprintf('%s ', Is115(ii,:));
P(ii,:,D) = input("");
end
% Computation for the percentage of students that are present in class.
CP(D,:) = (sum(P(:,:,D))./length(Is115)) .* 100;
if CP >= 60
fprintf("Congratulations! %.0f %% of the class is present\n", CP(length(CP),:));
elseif CP <60
fprintf("Better luck next time! %.0f %% of the class is present\n", CP(length(CP),:));
end
% Plotting percentage
x = 1:length(CP);
y = round(CP(x,:));
if length(y) < 2
% states color map
map = colormap('pink');
% extracts rows and columns to mmap and nmap
[mmap,nmap] = size(map);
% declares min and max y values
data_min = 0;
data_max = max(y);
f = figure(1), hold on
for k = 1:length(y)
h=bar(x(k),y(k));
% now define col value based on data value (min data value maps to
% colormap map index 1
% and max data value maps to colormap map last index);
ind = fix(1+(mmap-1)*(y(k)-data_min)/(data_max-data_min));
set(h, 'FaceColor', map(ind,:));
% Display the values as labels at the tips of the bars.
xtips1 = h.XEndPoints;
ytips1 = h.YEndPoints + 3;
labels1 = string(h.YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center')
end
xlabel('X axis')
ylabel('Y axis')
hold off
grid on
CBAR_ticks = (0:20:100);
clim([min(CBAR_ticks),max(CBAR_ticks)])
hcb=colorbar('Ticks',CBAR_ticks,'TickLabels',split(num2str(CBAR_ticks)));
hcb.Title.String = "Y range";
hcb.Title.FontSize = 13;
else
map = colormap('pink');
[mmap,nmap] = size(map);
data_min = min(y);
data_max = max(y);
f = figure(1), hold on
for k = 1:length(y)
h=bar(x(k),y(k));
ind = fix(1+(mmap-1)*(y(k)-data_min)/(data_max-data_min));
set(h, 'FaceColor', map(ind,:)) ;
xtips1 = h.XEndPoints;
ytips1 = h.YEndPoints + 3;
labels1 = string(h.YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center')
end
xlabel('X axis')
ylabel('Y axis')
hold off
grid on
CBAR_ticks = 10*(fix(min(y)/10):ceil(max(y)/10));
clim([min(CBAR_ticks),max(CBAR_ticks)])
hcb=colorbar('Ticks',CBAR_ticks,'TickLabels',split(num2str(CBAR_ticks)));
hcb.Title.String = "Y range";
hcb.Title.FontSize = 13;
end
0 Comments
Accepted Answer
Walter Roberson
on 27 Sep 2022
Moved: Walter Roberson
on 27 Sep 2022
CP(D,:) = (sum(P(:,:,D))./length(Is115)) .* 100;
CP has not been initialized before that point.
D = input("DAY #");
D is user input, and could certainly be greater than 1. So when you assign to CP(D,:) you are creating CP (because it is not assigned before this) and all rows before the row indicated by D will be set to 0.
if CP >= 60
You are testing the entire matrix CP, resulting in (probably) a 2D matrix of logical values. When you if a vector or array, the result is considered to pass the test only under the condition that all values being tested are non-zero (false is 0) -- equivalent to |if all(CP(:) >= 60) . Which is going to be false if D > 1 because of all those rows of zeros that got inserted.
0 Comments
More Answers (0)
See Also
Categories
Find more on Colormaps 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!