这里有一个问题要问“Matlab 中记录较少的部分的专家”:是否有一种(未记录的?)方法来确定图形已打开多长时间(即图形的“年龄”)?
figure; spy;
myfig=gcf;
age=get_age() %shoud output age of figure in some format
最佳答案
您可以使用以下机制:
首先,创建一个如下所示的小 Matlab 函数,将 CreationTime 属性附加到图形:
function setCreationTime(hFig,varargin)
hProp = addprop(hFig,'CreationTime');
hFig.CreationTime = now;
hProp.SetAccess = 'private'; %make property read-only after setting its initial value
hProp = addprop(hFig,'Age');
hProp.GetMethod = @(h,e) etime(datevec(hFig.CreationTime), clock); %compute on-the-fly
hProp.SetAccess = 'private'; %make property read-only
end
现在将此函数指定为所有新图形的默认 CreateFcn 回调函数:
set(0,'DefaultFigureCreateFcn',@setCreationTime)
就是这样 - 你已经完成了!
例如:
>> newFig = figure;
>> newFig.CreationTime
ans =
737096.613706748
>> ageInDays = now - newFig.CreationTime
ageInDays =
0.01625078368466347
>> ageDuration = duration(ageInDays*24,0,0)
ageDuration =
duration
00:23:24
>> ageString = datestr(ageInDays, 'HH:MM:SS.FFF')
ageString =
'00:23:24.068'
>> ageInSecs = newFig.Age
ageInSecs =
1404.067710354923808
关于Matlab 图 'age',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48597836/