How to reduce the file size of a saved histogram figure

조회 수: 8 (최근 30일)
Jérôme
Jérôme 2022년 9월 20일
댓글: Jérôme 2022년 9월 20일
In the following code, I have a histogram showing 100 bins, therefore the amount of data shown in this figure is quite small. However, the file saved is about 154 MB, and using the "compact" option it is still 77 MB.
Therefore, it seems the figure still contains the original data, which can indeed be seen using the Property inspector (see attachment).
Is it possible to save only the histogram data, and not the original data, such that the file saved has a minimal size?
x = randn(10^7,1);
h = figure;
histogram(x, 100)
savefig(h, "test.fig")
savefig(h, "test2.fig", "compact")

채택된 답변

Steven Lord
Steven Lord 2022년 9월 20일
You could avoid creating the histogram using the data by specifying 'BinCounts' and 'BinEdges'. If you do this, there won't be a way to retrieve the original data from the histogram the way you could if you created it by passing the unbinned data into the function, nor would you be able to manipulate it using functions like morebins or fewerbins or by manually changing some of the bin related properties.
Create a histogram from data
cd(tempdir)
x = randn(10^7,1);
f = figure;
h = histogram(x, 100);
saveas(f, 'FigureWithData.fig');
Create a histogram from counts and edges
f2 = figure;
[values, edges] = histcounts(x, 100);
h2 = histogram('BinCounts', values, 'BinEdges', edges);
saveas(f2, 'FigureWithoutData.fig');
Show the sizes of the files
D = dir('*.fig');
for thefile = 1:numel(D)
fprintf("File %s has size %d.\n", D(thefile).name, D(thefile).bytes)
end
File FigureWithData.fig has size 154129315. File FigureWithoutData.fig has size 27555.
Try manipulating the count-and-edges histogram
f3 = figure;
h3 = histogram('BinCounts', values, 'BinEdges', edges);
morebins(h3) % This will error
Error using matlab.graphics.chart.primitive.Histogram/morebins
morebins function is not supported when 'BinCountsMode' value is 'manual'.
  댓글 수: 1
Jérôme
Jérôme 2022년 9월 20일
Thank you for the detailed example. This is exactly what I wanted, I don't need the original data from the histogram, I just want the plot.

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

추가 답변 (1개)

Rik
Rik 2022년 9월 20일
편집: Rik 2022년 9월 20일
You have two options that I'm aware of:
  1. Change the underlying data so you reproduce the same bins with fewer data points.
  2. Change to a bar chart (use histcounts to extract the counts per bin).

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by