How to remove border from MATLAB figure
Show older comments
I'm trying to compare spectrogram images in a MATLAB image analyzer, but I think the white border is causing them to be overly similar. Because of the number of images I need to process, I'd really like to have it automatically generate and save the image. Here is my current code that I’m using to make and save the spectrogram.
base=filename %The code saves multiple images with the label being the filename and a specific addition to each image
figure(1003)
spectrogram(Xacc,windowx,noverlap,nfft,fs,'yaxis')
ylim([0 5])
colormap(gray(256));
caxis([-160 40])
% title('Spectrogram of X')
s1=base + "SPEC_Acc_X GS";
saveas(gcf,s1,'jpg')
When I run it I get an image like this.

What I want is an image like this, but in order to get it I had to adjust every setting manually in the image editor. Alternately, is there a way to automatically crop saved images? That could also be a solution.

Thanks so much for the help!
Answers (1)
It might be easier to do that when you first create the spectrogram.
Try something like this —
Fs = 1000;
L = 10;
t = linspace(0, Fs*L, Fs*L+1).';
s = sum(sin(2*pi*t*(1:75:490)),2)*1E+3;
figure
spectrogram(s,[],[],[],Fs,'yaxis')
colormap(gray)
title('Original')
figure
spectrogram(s,[],[],[],Fs,'yaxis')
colormap(gray)
hf = gcf;
hcb = findobj(hf.Children,'Type','ColorBar');
hcb.Visible = 'off';
Ax = gca;
Ax.Visible = 'off';
title('Edited')
% get(hf)
.
2 Comments
Otto Randolph
on 11 Sep 2024
Star Strider
on 11 Sep 2024
My pleasure!
I wasn’t certain what you wanted, so I took my best guess. I’m happy to have been able to suggest a solution!
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
