create an interactive bar graph in matlab

main.m
x = [1, 2, 3, 4, 5];
y = [10, 20, 15, 25, 30];

figure;
bar(x,y);
title('Interactive Bar Graph Example');
xlabel('X-axis');
ylabel('Y-axis');

dcm_obj = datacursormode(gcf);
set(dcm_obj, 'UpdateFcn', @customdatalabel);

function output_txt = customdatalabel(~, event_obj)
    pos = get(event_obj, 'Position');
    output_txt = {['X: ', num2str(pos(1))], ['Y: ', num2str(pos(2))]};
end
385 chars
17 lines

Make sure to copy and paste the code into a MATLAB script file (.m) and run it to display the interactive bar graph. When you move your cursor over the bars on the graph, data labels will appear showing the exact position of each bar.

gistlibby LogSnag