Search code examples
matlabmatlab-uitable

Table data is not editable at this location, why won't this checkbox let me check it?


f = figure;
 columnname = {'X' , 'Y'};
 Selection = {'A','B','C','D','E'}
[vals{1:numel(Selection),1}]=deal(false)
    columnform = {'logical','logical'};
t = uitable('Data',vals,'ColumnName', columnname, 'ColumnFormat', columnform,'ColumnEdit',[true true true], 'RowName', Selection);

If you run this script it should spit out a figure. but i can only select the checkboxes in the X column of the table. Why is that?


Solution

  • Tha's because the variable vals does not contain sufficient data to fill the table.

    Simple replicate it to form a 2-column array and it works. i.e. add this line:

    vals = [vals vals]
    

    before creating the table.

    Whole code:

    f = figure;
    close all
    clc
    clear
    
    columnname = {'X' , 'Y'};
    Selection = {'A','B','C','D','E'}
    [vals{1:numel(Selection),1}]=deal(false);
    
    vals = [vals vals]
    
    columnform = {'logical','logical'};
    
    t = uitable('Data',vals,'ColumnName', columnname, 'ColumnFormat', columnform, 'RowName', Selection,'ColumnEdit',true(1,2*size(Selection,1)));
    

    Output:

    enter image description here

    EDIT:

    To get indices of selected cells you don't need to specifically add new variables.

    By setting the CellSelectionCallback you can fetch the indices directly.

    Let's say you add this line after creating the table:

    set(t,'CellSelectionCallback',@SelCB)
    

    Then the function can be used to get indices as follows:

    function SelCB(~, event)
    
        SelectedCells = event.Indices        
     end
    

    Which will output a 1x2 element vector each time a cell is selected/deselected.