7

这是“Matlab 记录较少部分的专家”的问题:是否有(未记录的?)方法来确定一个数字的开放时间(即数字的“年龄”)?

figure; spy;
myfig=gcf;
age=get_age() %shoud output age of figure in some format
4

3 回答 3

8

您可以使用以下机制:

首先,创建一个小的 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
于 2018-02-05T12:50:09.963 回答
5

据我所知,图形对象不会公开此类信息。甚至在其未记录的基础Java类中也没有。但我有一个想法可能代表了这个问题的一个很好的解决方法。

使用图形函数的以下重载:

figure(n) 查找 Number 属性等于 n 的图窗,并将其设为当前图窗。如果不存在具有该属性值的图窗,MATLAB® 将创建一个新图窗并将其 Number 属性设置为 n。

为了为每个现有图形分配一个“唯一标识符”,并将这些标识符与datenum代表创建时间的值相关联:

% Initialize the lookup table somewhere in your code:
lookup_table = zeros(0,2);
% Together with a variable that stores the next unique identifier to be assigned:
next_id = 1;

% When you need to instantiate a new figure...
    % 1) Retrieve the current datetime:
    cdate = now();
    % 2) Update the lookup table:
    lookup_table = [lookup_table; next_id cdate];
    % 3) Initialize the new figure:
    figure(next_id);
    % 4) Increment the next unique identifier:
    next_id = next_id + 1;

然后,查找表的每一行都将包含一个唯一的图形标识符及其各自的创建日期。

其他一切都很容易处理。当你想查询一个数字正常运行时间...在查找表中找到它的唯一标识符并将当前时间(使用now()命令获得)减去创建时间。我建议您为CloseRequestFcn您创建的每个图形定义一个句柄,以便当图形关闭时,您可以更新lookup_table并删除它。您分配给特定图形的标识符可以使用其Number属性检索。这是一个完整的工作实现:

global lookup_table;
global next_id;

lookup_table = zeros(0,2);
next_id = 1;

f1 = create_figure();
f2 = create_figure();

pause(10);

f1_ut = get_figure_uptime(f1)
f2_ut = get_figure_uptime(f2)

function f = create_figure()
    global lookup_table;
    global next_id;

    cdate = now();

    f = figure(next_id);
    f.CloseRequestFcn = @update_lookup_table; 

    lookup_table = [lookup_table; next_id cdate];
    next_id = next_id + 1;
end

function ut = get_figure_uptime(f)
    global lookup_table;

    tn = now();
    tc = lookup_table(lookup_table(:,1) == f.Number,2);

    if (isempty(tc))
        ut = -1;
    else
        ut = etime(datevec(tn),datevec(tc));
    end
end

function update_lookup_table(f,~)
    global lookup_table;
    lookup_table(lookup_table(:,1) == f.Number,:) = [];

    delete(f);
end

或者,正如您在评论中建议的那样,您可以为您创建的每个图形添加一个属性,其中可以存储其创建时间。它更加直接,并且无需处理查找表。为此,只需使用addprop 函数,如下所示:

cdate = now();

f = figure();
addprop(f,'CreationTime');
f.CreationTime = cdate;
于 2018-02-03T13:47:05.037 回答
3

请参阅此链接 您的问题的答案可能是此代码(这将适用于 MATLAB R2014b 及更高版本)。我用 R2015a 测试过。

figure; spy;
my_fig=groot;
cnt = 0;
pause(0.05)
while ~isempty(my_fig.Children)
    cnt=cnt+1
    pause(0.01)
end

这里 cnt 值将与时间成正比,窗口在关闭之前存在。笔记:

  1. 可以减少 pause 的参数值,在计算时间时,要考虑暂停时间
  2. 代替计数器方法的cnt变量,可以使用系统时钟时间。
于 2018-02-03T17:23:40.373 回答