Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In matlab how can I save a histogram from command line?

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?

like image 400
user1505100 Avatar asked Jul 05 '12 20:07

user1505100


People also ask

How do you save a histogram in Matlab?

Use the savefig function to save a histogram figure. Use openfig to load the histogram figure back into MATLAB. openfig also returns a handle to the figure, h . h = openfig('histogram.

Which command is used to plot histogram Matlab?

hist( x ) creates a histogram bar chart of the elements in vector x . The elements in x are sorted into 10 equally spaced bins along the x-axis between the minimum and maximum values of x . hist displays bins as rectangles, such that the height of each rectangle indicates the number of elements in the bin.

How do I save a plotted image in Matlab?

You can save plots as images or as vector graphics files using either the export button in the axes toolbar, or by calling the exportgraphics function.

How do you save a histogram as a picture?

Plot the histogram using hist() method. To save the histogram, use plt. savefig('image_name').


2 Answers

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)
like image 178
tmpearce Avatar answered Sep 24 '22 07:09

tmpearce


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.

like image 35
Dan Becker Avatar answered Sep 22 '22 07:09

Dan Becker