1

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

Fatime
  • 197
  • 1
  • 5
  • 15
  • possible duplicate of [How can I add labels (of values) to the top of my MATLAB plot?](http://stackoverflow.com/questions/12501432/how-can-i-add-labels-of-values-to-the-top-of-my-matlab-plot) and [How to display labels above a histogram bin?](http://stackoverflow.com/questions/4657719/how-to-display-labels-above-a-histogram-bin) – Eitan T Sep 17 '13 at 12:03
  • I suggest the second question to this post is asked as a separate one. I believe more than you would like to know this, and it would be easier to find the question if it had a different heading. – Stewie Griffin Sep 17 '13 at 12:23
  • @EitanT Thanks, but I want to change the saved .fig files. I think my question is different from what you mentioned. – Fatime Sep 17 '13 at 12:29
  • @Fatime So what you're saying is that you have the .fig files but not code generating them? – Eitan T Sep 17 '13 at 12:31
  • 1
    The edit came after my answer. If the answer to @EitanT's question is yes, I believe it would be better to ask the second part separately. (IMHO: As it stands now, the heading is misleading). – Stewie Griffin Sep 17 '13 at 12:37

2 Answers2

1

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.

Community
  • 1
  • 1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
1

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' );
marsei
  • 7,691
  • 3
  • 32
  • 41