将已过的时间计数器添加到等待栏中-Matlab [英] Add to waitbar an elapsed time counter - Matlab
问题描述
在下面的Matlab GUI代码中,我保存了TIFF图像并使用了waitbar函数,因为它有时需要花费几秒钟的时间,该bar不会更新,并且在保存文件时会自动关闭.是否可以添加经过时间的计数器来指示运行时间?
In the following Matlab GUI code I save a TIFF image and used the waitbar function since it take sometimes few seconds, the bar is not updating and is closing automatically when the file is saved. Is it possible to add an elapsed time counter to indicate time of operation?
代码段:
h = waitbar(0,'In process');
export_fig(handles.imageAxes,saveFileAs, '-r500');
close(h);
推荐答案
以下答案适用 tiff 特别是图像.
我试图找到imwrite
函数的一般解决方案,但找不到.
The following answer applies tiff images specifically.
I tried to find a general solution for imwrite
function, but I couldn't.
该解决方案使用 Tiff类,而不是使用imwrite
.
Tiff类允许逐条保存tiff图像文件,而不是一次保存整个图像.
请参阅: http://www.mathworks.com/help/matlab/ref/tiff-class.html
The solution uses Tiff class instead of using imwrite
.
Tiff class allows saving a tiff image file strip by strip, instead of saving entire image at once.
See: http://www.mathworks.com/help/matlab/ref/tiff-class.html
以下代码示例将保存一个(相对)较大的tiff图像文件,并在保存过程中显示一个前进的等待栏:
The following code sample saves a (relatively) large tiff image file, and display an advancing waitbar while saving is in progress:
%Simulate large image (to be saved as tiff later)
I = imread('peppers.png');
I = repmat(I, [10, 10]); %Image resolution: 5120 x 3840.
t = Tiff('I.tif', 'w');
width = size(I, 2);
height = size(I, 1);
rows_per_strip = 16; %Select 16 rows per strip.
setTag(t, 'ImageLength', height)
setTag(t, 'ImageWidth', width)
setTag(t, 'Photometric', Tiff.Photometric.RGB)
setTag(t, 'BitsPerSample', 8)
setTag(t, 'SamplesPerPixel', 3)
setTag(t, 'RowsPerStrip', rows_per_strip)
setTag(t, 'PlanarConfiguration', Tiff.PlanarConfiguration.Chunky)
setTag(t, 'Compression', Tiff.Compression.LZW)
n_strips = ceil(height / rows_per_strip); %Total number of strips.
h = waitbar(0, 'In process');
%Write the tiff image strip by strip (and advance the waitbar).
for i = 1:n_strips
y0 = (i-1)*rows_per_strip + 1; %First row of current strip.
y1 = min(y0 + rows_per_strip - 1, height); %Last row of current strip.
writeEncodedStrip(t, i, I(y0:y1, :, :)) %Write strip rows y0 to y1.
waitbar(i/n_strips, h); %Update waitbar.
drawnow %Force GUI refresh.
end
close(t)
close(h)
这篇关于将已过的时间计数器添加到等待栏中-Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!