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?
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.
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.
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.
Plot the histogram using hist() method. To save the histogram, use plt. savefig('image_name').
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With