How to save a figure's content as image?
Show older comments
I want to save the content of the figure without any other stuff like border, title etc.
For example, this is the figure.

But How to Save the Red Area Content Only???

2 Comments
Jan
on 14 Mar 2017
Please explain how you want to save the file: Through a command or by using the menus?
K M Ibrahim Khalilullah
on 19 Jun 2018
I am fetching the same problem...I want to save from menu..
Answers (2)
What about this:
F = getframe(gca);
imwrite(F.cdata, 'File.png');
Thios capture the contents of the currently active axes object. It is safer to provide teh axes' handle instead of gca.
8 Comments
John Wray
on 14 Mar 2017
Jan
on 14 Mar 2017
Are your sure? getframe with the axes handle snaps the contents of the axes only. Then what do you mean by "the blank area"?
Yasir Muhammad
on 29 Dec 2020
i have multiple figures about 50 how i save them all once
Walter Roberson
on 29 Dec 2020
That is not possible. All of the save commands can only save one at a time.
Exception: In the case where all of the figures are exactly the same size, you could create a 4d array and save that in a small number of different ways, such as writing a Dicom series file.
Yasir Muhammad
on 29 Dec 2020
that worked for me
D = 'C:\Users\Muhammad Yasir\Downloads\Compressed\project matlab\ch08';
S = dir(fullfile(D,'*.wav'));
N = numel(S);
y = cell(1,N);
fs = cell(1,N);
for k = 1:N
%%%%
code
%%%%%
name = sprintf('figure_%d.jpg',k);
% Specify some particular, specific folder:
path = fullfile('C:\Users\Muhammad Yasir\Desktop\ch08\', name);
saveas(gcf,path,'png')
end
Walter Roberson
on 30 Dec 2020
Note though that you do not do any graphics changes between frames, so each saveas(gcf) will save the same image.
Also, those y and fs are not doing you any good in that code.
And you are not reading the .wav files.
Amrutamaya Behera
on 3 Jan 2021
Edited: Amrutamaya Behera
on 3 Jan 2021
Why doesn't this getframe() command always work properly. In some cases i noted when my XLim or YLim is higher than 1000 this command gets only somepart of full frame. Please help me out.
Walter Roberson
on 3 Jan 2021
Are you passing a rectangle specfication to getframe ?
There is a challenge that axes size can change slightly depending on xlim / ylim; I have noticed a difference of between -1 to +2 pixels. It was not as simple as "limit over 1000 is 2 pixels bigger". I seem to recall that I traced it to a rounding issue, a round-trip calculation that algebraically "should" work out to convert pixels -> internal coordinates -> pixels, but which can give minor differences.
When used without a rectangle specification, the normal result of this is that the captured area is slightly different in size, and that becomes a problem for writing video frames, as videos expect constant size.
The work-around I developed a couple of months ago was to record the size of the first getframe() and thereafter imresize() whatever was captured to that size.
I have not experimented using the rectangle specification; I would think that it could result in pixels being chopped, and I do not know what would happen for the case where the size available for capture was smaller than the rectangle specification.
Walter Roberson
on 14 Mar 2017
The easiest way would probably to use
axis off
followed by saveas() or print()
2 Comments
John Wray
on 14 Mar 2017
Andrea Manconi
on 30 Sep 2019
axis off tight
This should make the trick!
Categories
Find more on White 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!