点击鼠标,matlab获取日期

时间:2012-12-11 15:55:32

标签: matlab matlab-uitable

我想从日历中获取两个日期。 matlab函数是

c = calendar

dates = calendar; 
dates(~any(dates,2),:) = []; 
fh = figure; 
uh = uitable('parent',fh,'data',dates,'ColumnWidth',repmat({20},1,7),...
          'ColumnName',{'S','M','T','W','T','F','S'});

但是如何让用户点击这两个日期。

1 个答案:

答案 0 :(得分:1)

创建回调函数:

function cell_select_callback(h, ind)
    dates = get(h, 'data');
    d = dates(ind.Indices(1), ind.Indices(2));
    if d == 0
        return
    end
    dn = datenum(2012, 12, d); % you have to have year & month here
    fprintf('cell_select_callback(): click at [%d %d] on %s\n', ind.Indices(1), ind.Indices(2), datestr(dn));
return

并添加到uitable()参数中:uitable(..., 'CellSelectionCallback', @cell_select_callback)

按下时,cell_select_callback将打印点击的坐标和日期,例如: cell_select_callback(): click at [2 3] on 04-Dec-2012