Search code examples
matlabmatlab-uitable

Get date by mouse click, matlab


I want to get two dates from a calendar. matlab functio is

c = calendar

or

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'});

but how to get these two dates clicked by user.


Solution

  • Create a callback function:

    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
    

    and add into uitable() arguments: uitable(..., 'CellSelectionCallback', @cell_select_callback).

    When pressed, cell_select_callback will print the coordinates of the click and the date, for example: cell_select_callback(): click at [2 3] on 04-Dec-2012