MATLAB: CellEditCallback of Uitable can’t get data of the table in a function

celleditcallbackguiuitable

It works perfect without the first row. But in a function,it cannot get data when cell is edited. The Command Window show that "Error using handle.handle/get Invalid or deleted object.
Error while evaluating uitable CellEditCallback "
function mytable
f = figure;
d = randi(100, 7, 3);
t = uitable(f,'Data',d,'ColumnWidth',{50},...
'ColumnEditable', [true true true]);
set(t, 'CellEditCallback', 'get(t,''Data'')');

Best Answer

If by 'without the first row' you mean the first row of code, i.e.
function mytable
then that is because without that line you have a script and unless you explicitly delete the variables they remain in your workspace, so that when the callback triggers, 't' still exists.
A function, as defined by that line, has its own enclosed workspace and when the last line of the function has been evaluated the function exits and its workspace is cleared out - i.e. all the variables defined in it go out of scope. So when you then access your table (which still exists itself because the figure still exists) the variable 't' which your callback tries to access no longer exists which causes this crash.
I'm not really familiar with the callback syntax you are using, with a string, as I generally use anonymous functions which embed the required workspace inside them even if it then goes out of scope. A 'get' function as a callback also doesn't seem to make much sense though because you cannot pick up an output argument from a callback.