display the percentages inside the graph in matlab

You can display the percentages inside a graph in MATLAB by first creating a pie chart or bar graph and then using the text function to display the percentages. Here's an example for a pie chart:

main.m
data = [25, 35, 20, 10, 10];
labels = {'A', 'B', 'C', 'D', 'E'};

pie(data, labels);
pct = 100 * data / sum(data);
text(pct.*cosd(cumsum([0; data(1:end-1)])*360/sum(data)), pct.*sind(cumsum([0; data(1:end-1)])*360/sum(data)), ...
    cellfun(@(x) sprintf('%.1f%%',x), num2cell(pct), 'UniformOutput', false), 'Color', 'white', 'FontSize', 10)
342 chars
8 lines

For a bar graph, you can add the percentages on top of each bar using the text function. Here's an example for a bar graph:

main.m
data = [25, 35, 20, 10, 10];
bar(data);
text(1:length(data), data, arrayfun(@(x) [num2str(x) '%'], data, 'UniformOutput', false), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom')
194 chars
4 lines

related categories

gistlibby LogSnag