Fatime
Fatime

Reputation: 197

Matlab, histogram plot data-value of bar

I want to display the value of each bar in histogram plot in matlab. I save all the plots as matlab .fig files. How to change the figures? any Idea?

Thanks

Upvotes: 1

Views: 5943

Answers (2)

marsei
marsei

Reputation: 7751

Here is some code to get the Y data from a .fig file (with bar series), and then display the corresponding text. The Y data is buried in the children of the current axes - we need to apply the get command twice.

%create figure
h = figure('Color','w');
x =rand(10,1);
bar(x(:,1));
set(gca,'XLim', [0 11], 'YLim', [0 1]);
saveas(h,'myfig.fig');
close(h);

%open figure, get the bar data, then text
open('myfig.fig');
xdata = get(get(gca,'Children'), 'xData')
ydata = get(get(gca,'Children'), 'YData')
text(xdata, ydata, num2str(ydata',2), 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom' );

Upvotes: 1

Stewie Griffin
Stewie Griffin

Reputation: 14939

It might not be perfect, but it's a start:

x =rand(10,1);
bar(x(:,1));
text(1:10,x,num2str(x))

enter image description here

Update: If you wanted a histogram and not bars:

x =ceil(10*rand(30,1));
hist(x);
a = hist(x);

% This can most likely be done without a loop, but here goes:
for ii = 1:10
    text(ii,a(ii),num2str(a(ii)))
end

enter image description here

You can offset the numbers by adding assigning the text at a(ii)+0.1, or something similar. Other than that, see this answer by Eitan, to get some tips and tricks.

Upvotes: 1

Related Questions