1

I'm working at GUI in MATLAB application. I use uitable object. Then I find interesting undocumented feature how to sort it's data, select whole row and etc.

I do this way:

% create jhandle to my uitable object
juiTable = findjobj(handles.uitable1,'class','UIScrollPane');
jtable = juiTable(1).getComponent(0).getComponent(0);
%... some my action like this:
jtable.setRowSelectionAllowed(true);
%...

%and now lets try use callback for selected cell in uitable:
juiFunHandle = handle(jtable, 'CallbackProperties');

set(juiFunHandle, 'MousePressedCallback', @CellSelectionCallback);
set(juiFunHandle, 'KeyPressedCallback', @CellSelectionCallback); 

That works perfectly.

Now question: how to put multiple parameters to CellSelectionCallback? I want this function makes some action (makes some button active etc). For this I try to put GUI handles to it. But how?

My CellSelectionCallback function:

function CellSelectionCallback(juiTable, varargin)
% get it from the example
row = get(juiTable,'SelectedRow')+1;
fprintf('row #%d selected\n', row);

P.S. I see varargin into it. So can I use multiple arguments? How to put it using my set function??

Mikhail_Sam
  • 10,602
  • 11
  • 66
  • 102

1 Answers1

1

By default, MATLAB callbacks pass two input arguments (the objec that generated the callback and some event data). If you want to pass more (or fewer) arguments to your callback, you can use an anonymous function to accept these two inputs and then call your callback with the desired inputs.

In your case, you could write your anonymous function such that you pass the handles object as an additional input to your callback function

set(juiFunHandle, 'MousePressedCallback', ...
    @(src, evnt)CellSelectionCallback(src, evnt, handles));

Then your callback would look something like:

function CellSelectionCallback(jtable, evntdata, handles)
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Ah, of course! Just use it like an ordinary anonymous function! By the way I can use `function CellSelectionCallback(juiTable, varargin)` and then `varargin{2}`. – Mikhail_Sam Oct 26 '16 at 12:58
  • @Mikhail_Sam Sure you can do that as well, I just prefer explicit inputs instead of `varargin` so I've shown that as an alternative approach. – Suever Oct 26 '16 at 12:59