1

I am using the MATLAB 2014a uitable and with the 'CellEditCallback', I create create a new figure by clicking a cell from my uitable. The problem is that the user may select multiple cells at the same time, then my program will open as much figures as the cells selected.

So I would like to know if it is possible to disable the uitable cell multiple selection. If not, do you have any suggestions to solve my issue ?

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
oro777
  • 1,110
  • 1
  • 14
  • 29
  • Have a look at [this question](http://stackoverflow.com/questions/19634250/how-to-deselect-cells-in-uitable-how-to-disable-cell-selection-highlighting). There are two things which could be useful for you. First: make a new uitable for each row: you can select just one. Second, alternatively: introduce a popup-menu for every cell and evaluate what is chosen there. There can be just popup at a time, so it would work also. May I write an answer later, but not sure if I have time. – Robert Seifert Aug 19 '14 at 08:00
  • I understand the use of uitable for each row, I am not sure to understand about the popup. Anyway, it may not work in my case because I have a lot of rows so if I create one uitable for each row, it will not automatically create a scroll, am I wrong ? – oro777 Aug 20 '14 at 01:31
  • no that won't work then. The idea with popup is, that you make the cell the user is supposed to click editable and you give him multiple options like `selector = { 'open figure window'; 'print figure directly' ; 'make coffee' };` - the user choose and the the `'CellEditCallback'` is triggered and you check what option was chosen and evaluate. Because he can just edit one cell at once, it should work as desired. PS: you can set initial value of the cell (the displayed value) as before, it doesn't affect the popup menu. – Robert Seifert Aug 20 '14 at 06:13

2 Answers2

1

I'm aware this is 3 years old but I found a simple solution that worked for me that won't interfere with callbacks - and more importantly doesn't require callbacks to "de-select". I figured someone will benefit from this.

I'm using MATLAB2017a but the functionality to leverage is in the underlying JAVA object so should work with older versions (down to 2008).

You simply access the underlying Java table object and change the selection mode to SINGLE_SELECTION. For this we all need to thank Yair on his work on accessing the underlying Java table object and more importantly sharing it on the MATLAB file exchange (search for "findjobj" -- note the letter "J" in the middle!).

This method works weather you instantiated the MATLAB uitable through uitable function or through implimenting it on guide editor. You just have to pass in the handle to the matlab table object (note: there's a distinction between that and the underlying java table object!) into the above mentioned findjobj function from MATLAB file exchange and configure the table in JAVA.

So the underlying JAVA feature we want to adjust is this

http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/JList.html#setSelectionMode(int)

Here's an example code that I verified using MATLAB 2017a on a 64 bit Windows machine:

% create a figure instance
h_fig = figure();

% Instantiate MATLAB's uitable
h_m_table = uitable( h_fig, ...
                    'Data', magic(3), ...
                    'ColumnName', {'A','B','C'} );

% if you already created a table using MATLAB's GUIDE editor, simply pass
% in the "tag" name property, which should be in the "handles" structure by
% default. If you didn't edit that field it's "uitable1" by default so:
% 
% h_m_table = handles.uitable1  % replace 'uitable1' with tag name

% Get java scroll pane object
j_scrollpane = findjobj(h_m_table);

% Get java table object
j_table = j_scrollpane.getViewport.getView;

% (optional) Make entire ROW highlighted when user clicks on any row(s)
j_table.setNonContiguousCellSelection(false);
j_table.setColumnSelectionAllowed(false);
j_table.setRowSelectionAllowed(true);

% Set selction mode to SINGLE_SELECCTION
j_table.setSelectionMode(0);

Now you get a figure with a table on it and you can select only one row at a time by clicking.

o_gilmour
  • 36
  • 5
0

I found this thread and findjobj doesn't work on App Designer, so I needed to do some more searching. I never found a way to directly deselect a cell on a uitable in App Designer - but I found a way to override the current table with a new one without the cell selections. my code

Armali
  • 18,255
  • 14
  • 57
  • 171
Nico
  • 1