Search code examples
matlabmatlab-figure

Saving a figure without axes, legend or title in Matlab


I am using matlab to convert a signal into a spectrogram which I then save as a png image file.

This is what I currently have:enter image description here

I would like to only have the plot, no border, axes or legend.

(Bonus question: how to I get a greyscale spectrogram?)

Here is the code I use:

for index = 1:Nb_Samples
    %Loads the data from the current example
    Time_Data = Data4{1,index}.time;
    Signal_Data = Data4{1,index}.values;

    figure(1)

    %Generates the spectrogram for the current example
    spectrogram(Signal_Data,hamming(CustomHammingWindowSize1),CustomHammingWindowSize1/2,(PatchWindowSize^2-1)*2,1000,'yaxis')

    h=gcf;
    set(h,'PaperPositionMode','auto');
    set(h,'PaperOrientation','landscape');
    set(h,'Position',[20 20 1100 700]);
    print(gcf, '-dpng', 'Spectro.png')
end

Solution

  • The best way would be to directly save the data, without plotting it e.g. using imwrite, as anything you used to plot that must be a NxM matrix, i.e. an image. As @CrisLuengo says in the comments, just grab spc=spectogram(... the output, scale it to [0-1] and save it using imwrite for the best results.

    I would not recommend it for this case, but you can always colormap(gray), axis off and well, just not call colorbar (or colorbar('off')), to remove the extra stuff in the figure. But do good science! Save the data properly, don't introduce artifacts by adding the extra step of plotting.