1
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?

excelhelp
  • 91
  • 8

1 Answers1

1

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.

Benoit_11
  • 13,905
  • 2
  • 24
  • 35
  • thanks for the help, how do I resize the columns so that they fit to the data and there isn't that large blank space on the right and bottom? Also is there a way to have only 1 box checked per column at a time or will i have to switch to radio buttons for that? – excelhelp Mar 16 '15 at 17:22
  • I can change the column width for the individual variables like x and y just fine, but that blank space jsut wont leave or automatically resize. I'll keep looking I guess. – excelhelp Mar 16 '15 at 18:15
  • It looks like you can adjust the table position using a 4-element vector `[x y width height]` using `set(t,'Position',[])` – Benoit_11 Mar 16 '15 at 18:21