7

I have a large number of files that I need to make histograms for therefore I want to save it from the command line. For plots I usually save it in matlab using the following command:

figure = plot (x,y)
saveas(figure, output, 'jpg')

I want to do the same for histograms:

figure = hist(x)
saveas(figure, output, 'jpg')

However I get an error that says incorrect handle. I also tried imwrite function, the code executes but saves a blank black image. Is there a way in which I will be able to save my histograms?

Eitan T
  • 32,660
  • 14
  • 72
  • 109
user1505100
  • 71
  • 1
  • 2

3 Answers3

13

When you use hist with an output argument, it returns the count for each bin, not a handle object like the other types of plots you're used to.

Instead, grab a handle to a figure, use hist with no output args to plot into the figure, then save the figure.

fh = figure;
hist(x);
saveas(fh, output, 'jpg')
close(fh)
Abhinav
  • 1,882
  • 1
  • 18
  • 34
tmpearce
  • 12,523
  • 4
  • 42
  • 60
1

export_fig from the MATLAB file exchange handles this for you automatically, and has other nice features as well. For an example of how to use it see another answer of mine here.

Community
  • 1
  • 1
Dan Becker
  • 1,196
  • 10
  • 18
0
fh = figure;
imhist(x);
saveas(fh, 'output', 'jpg');
Samin
  • 1