How to remove border from MATLAB figure

조회 수: 24 (최근 30일)
Otto Randolph
Otto Randolph 2024년 9월 10일
댓글: Star Strider 2024년 9월 11일
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!

답변 (1개)

Star Strider
Star Strider 2024년 9월 10일
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
Otto Randolph
Otto Randolph 2024년 9월 11일
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])
Star Strider
Star Strider 2024년 9월 11일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by