Define bar3() colors with hold on

3 views (last 30 days)
newbie9
newbie9 on 25 Mar 2018
Commented: Star Strider on 26 Mar 2018
I am trying to control the colors in a bar3() plot using hold on, but I keep over-riding my previous plot.
My code is below. Goal image is shown as 1st JPEG, and current Matlab plot is second JPEG. Data are also uploaded as text file.
fid001 = fopen('T001_RP2_Deagg_Combined_mod.txt', 'r');
header = textscan(fid001, '%s %s %s %s %s %s', 1 , 'HeaderLines', 0);
data = textscan(fid001, '%f %f %f %f %f %f', 'Delimiter', '\t');
fclose(fid001);
data = cell2mat(data);
Bin1_BeginRow = 1;
Bin1_EndRow = max(data(:,1)) * max(data(:,5));
Bin2_BeginRow = Bin1_EndRow + 1;
Bin2_EndRow = Bin2_BeginRow + Bin1_EndRow - 1;
Bin3_BeginRow = Bin2_EndRow + 1;
Bin3_EndRow = Bin3_BeginRow + Bin1_EndRow - 1;
Bin4_BeginRow = Bin3_EndRow + 1;
Bin4_EndRow = Bin4_BeginRow + Bin1_EndRow - 1;
Bin5_BeginRow = Bin4_EndRow + 1;
Bin5_EndRow = Bin5_BeginRow + Bin1_EndRow - 1;
Bin6_BeginRow = Bin5_EndRow + 1;
Bin6_EndRow = Bin6_BeginRow + Bin1_EndRow - 1;
%%%%%%%%%%Bin1
data1 = data(Bin1_BeginRow:Bin1_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
bar3(data2)
b = bar3(data2);
set(b,'FaceColor',[1 0 1]); % magenta
hold on
clear data1 data2 i xx yy zz
%%%%%%%%%%Bin2
data1 = data(Bin2_BeginRow:Bin2_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
b = bar3(data2);
set(b,'FaceColor',[0 0 1]); % blue
clear data1 data2 i xx yy zz
%%%%%%%%%%Bin3
data1 = data(Bin3_BeginRow:Bin3_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
b = bar3(data2);
set(b,'FaceColor',[0 1 0]); % green
clear data1 data2 i xx yy zz
%%%%%%%%%%Bin4
data1 = data(Bin4_BeginRow:Bin4_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
b = bar3(data2);
set(b,'FaceColor',[1 1 0]); % yellow
clear data1 data2 i xx yy zz
%%%%%%%%%%Bin5
data1 = data(Bin5_BeginRow:Bin5_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
b = bar3(data2);
set(b,'FaceColor',[0 1 1]); % cyan change to orange
clear data1 data2 i xx yy zz
%%%%%%%%%%Bin6
data1 = data(Bin6_BeginRow:Bin6_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
b = bar3(data2);
set(b,'FaceColor',[1 0 0]); % red
clear data1 data2 i xx yy zz
XBins = {'5.0 - 5.5'; '5.5 - 6.0'; '6.0 - 6.5'; '6.5 - 7.0'; '7.0 - 7.5'; '7.5 - 8.0'; '8.0 - 8.5'; '8.5 - 9.0'};
set(gca,'xtick',[1:8],'xticklabel',XBins)
YBins = {'0 - 5'; '5 - 10'; '10 - 15'; '15 - 20'; '20 - 25'; '25 - 30'; '30 - 40'; '40 - 50'; '50 - 75'; '75 - 100'; '100 - 200'};
set(gca,'ytick',[1:11],'yticklabel',YBins)
xtickangle(290)
ytickangle(30)
hold off;

Accepted Answer

Star Strider
Star Strider on 25 Mar 2018
It is necessary to reverse the order:
%%%%%%%%%%Bin1
data1 = data(Bin6_BeginRow:Bin6_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
bar3(data2)
b = bar3(data2);
set(b,'FaceColor',[1 0 1]); % magenta
hold on
clear data1 data2 i xx yy zz
%%%%%%%%%%Bin2
data1 = data(Bin5_BeginRow:Bin5_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
b = bar3(data2);
set(b,'FaceColor',[0 0 1]); % blue
clear data1 data2 i xx yy zz
%%%%%%%%%%Bin3
data1 = data(Bin4_BeginRow:Bin4_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
b = bar3(data2);
set(b,'FaceColor',[0 1 0]); % green
clear data1 data2 i xx yy zz
%%%%%%%%%%Bin4
data1 = data(Bin3_BeginRow:Bin3_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
b = bar3(data2);
set(b,'FaceColor',[1 1 0]); % yellow
clear data1 data2 i xx yy zz
%%%%%%%%%%Bin5
data1 = data(Bin2_BeginRow:Bin2_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
b = bar3(data2);
set(b,'FaceColor',[0 1 1]); % cyan change to orange
clear data1 data2 i xx yy zz
%%%%%%%%%%Bin6
data1 = data(Bin1_BeginRow:Bin1_EndRow, :);
data2 = zeros(max(data1(:, 1)), max(data1(:, 5)));
for i = 1:length(data1)
xx = data1(i, 1);
yy = data1(i, 5);
zz = data1(i, 4);
if zz > data2(xx, yy)
data2(xx, yy) = zz;
end
end
b = bar3(data2);
set(b,'FaceColor',[1 0 0]); % red
clear data1 data2 i xx yy zz
XBins = {'5.0 - 5.5'; '5.5 - 6.0'; '6.0 - 6.5'; '6.5 - 7.0'; '7.0 - 7.5'; '7.5 - 8.0'; '8.0 - 8.5'; '8.5 - 9.0'};
set(gca,'xtick',[1:8],'xticklabel',XBins)
YBins = {'0 - 5'; '5 - 10'; '10 - 15'; '15 - 20'; '20 - 25'; '25 - 30'; '30 - 40'; '40 - 50'; '50 - 75'; '75 - 100'; '100 - 200'};
set(gca,'ytick',[1:11],'yticklabel',YBins)
xtickangle(290)
ytickangle(30)
hold off
This should do what you want.
  4 Comments
newbie9
newbie9 on 26 Mar 2018
Perfect!! Many thanks!
Star Strider
Star Strider on 26 Mar 2018
Thank you! Again, my pleasure!
Yours was an interesting problem!

Sign in to comment.

More Answers (0)

Categories

Find more on Text Analytics Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!