0

I wish to create an if statement that will check if a specific cells of a logical column inside a uitable are true or false. Therefore, how can I check the logical cell status (upon clicking a push button, not shown)?

Script:

% Table
c1 = rand(10,3);
h = uitable('Units','normalized', 'Position',[0 0 1 1], 'Data',c1);

%# add new column of check boxes to table
c2 = c1(:,1)>0.5;
set(h, 'Data',[num2cell(c1) num2cell(c2)], 'ColumnFormat',[repmat({[]},1,size(c1,2)),'logical'], 'ColumnEditable',[false(1,size(c1,2)),true])
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
lroca
  • 621
  • 2
  • 8
  • 19
  • Not sure I'm getting the problem,... you can get the current table content at any time via `get(h, 'Data')`, just in the very same way as you're setting it in the above code. Is there a problem with that? – sebastian Dec 11 '13 at 10:43
  • I want to know the row numbers of a clicked checkboxes (column 4). – lroca Dec 11 '13 at 10:46
  • you need to set the `CellSelectionCallback`, it returns the indices of row and column. In the callback funtion you then get the complete data by `get(yourUItablehandle, 'Data')` and adress it with the indices you got before. – Robert Seifert Dec 11 '13 at 11:00
  • Thanks. Can you please explain me how can I use CellSelectionCallback. Is it possible to use it inside a Button_Callback as my main idea is to select rows that will be saved. Thanks again. – lroca Dec 11 '13 at 12:04
  • @thewaywewalk I guess that should rather be the `CellEditCallback` !? – sebastian Dec 11 '13 at 12:48
  • @Sebastian The question is very unclear regarding that. Probably your solution is the one the OP is looking for. – Robert Seifert Dec 11 '13 at 13:29

1 Answers1

1

If you want to get the selected rows in a button-callback, there's no need for the CellSelection/CellEditCallback.

As I suggested in my first comment, simply get the data and find the selected rows:

function button_callback(hObject, evt, handles)

    % get the data - identical to setting the data
    data = get(handles.tableHandle, 'Data');
    checkBoxColumn = 4;

    % logical indices of selected rows
    isRowSelected = [data{:, checkBoxColumn}];

    % if you want the indices
    idxSelectedRows = find(isRowSelected);
end
sebastian
  • 9,526
  • 26
  • 54
  • Thanks. I used the CellEditCallback as without it I cannot toggle off/on the checkboxes. Furthermore, it should work but instead of getting the row numbers I get how much rows (e.g., 1 2 3). Any idea why? – lroca Dec 11 '13 at 16:25
  • Not really. In my solution `isRowSelected` should be a logical vector with as many elements as your table has rows and `idxSelectedRows` should contain the indices of selected rows. Usually you should not need the CellEditcallback... – sebastian Dec 11 '13 at 17:01