How to remove border from MATLAB figure

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

Thanks so much for the help! That ended up not being the solution but you got me on the right track. Your solution hid the objects but I still had the wide margins. What ended up solving it was manually coding to delete the individual axis ticks and things and then changing the width of the image with this code.
colorbar('off')
set(gca,'XTick',[])
set(gca,'YTick',[])
xlabel('')
ylabel('')
set(gca,'position',[0,0,1,1])
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!

Sign in to comment.

Products

Release

R2021b

Asked:

on 10 Sep 2024

Commented:

on 11 Sep 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!