0
figure;
histogram = hist(np,180);
name=['histogram-' int2str(k) '.png'];  
%% k is the iterator so basically I want to save all the images using a loop.
imwrite(out,name);

The image I got is only a horizontal line. Does someone know how to fix this?

user3610523
  • 1
  • 1
  • 1
  • 1

1 Answers1

1

you can use savefig instead of imwrite

here is the doc http://www.mathworks.ch/ch/help/matlab/ref/savefig.html

savefig(h,filename)

h is the handle of figure. you could skip h to save the current figure.

(edit) savefig may not be there depending on the MATLAB version. In 2012b, it does not exit.

So saveas may be better:

f=figure;
hist([1 2 2 3]);
saveas(f, 'histogram-1.png')

This worked in MATALB 2012b. You can save it as .fig too.

otterb
  • 2,660
  • 2
  • 29
  • 48
  • I tried your method but it gave me an error "Undefined function 'savefig' for input arguments of type 'double'" – user3610523 May 14 '14 at 15:38
  • i see. probably you should get the handle from figure not from hist. So, fig = figure; and savefig(fig, "histogram-1.pgn"). I dont have matlab at moment so I am guessing. – otterb May 14 '14 at 15:45
  • I tried and failed again. And for the saveas command, I got an error "Invalid Simulink object handle" – user3610523 May 14 '14 at 15:47
  • huum. maybe try to get a handle this way http://stackoverflow.com/a/4540682/566035 – otterb May 14 '14 at 15:51