create a bar graph with colors indicator from two values, one value for the bar graph, one value for the color (if pvalue > 0, the color is red, etc) in matlab

Assuming you have two sets of values y and c, where y corresponds to the height of the bars and c corresponds to a color value for each bar based on some criteria (for example, if c(i) > 0, the bar should be red):

main.m
% example y and c values
y = rand(1,5);
c = randn(1,5);

% create figure window and axes
figure;
b = bar(y);

% set bar colors based on c values
for i = 1:length(c)
    if c(i) > 0
        b(i).FaceColor = 'r';
    else
        b(i).FaceColor = 'b';
    end
end
262 chars
17 lines

This will create a bar graph with bars of height y and colors dependent on the values in c. In this example, if a value in c is greater than 0, the corresponding bar is red, otherwise it is blue.

related categories

gistlibby LogSnag