MATLAB: Uitable CellEditCallback works in a script and fails in a function. How to manage uitable within a user function

MATLABuitable function use callback

The following script executes and returns changed data:
tdata = {};
fig = uifigure('Position',[404 390 600 420]);
%

myData = {48000 30 10.6 'Choose','Enter Filename','false'};
%
uit = uitable('Parent', fig, ...
'Position', [20 350 550 60], ...
'ColumnFormat',({[] [] [] {'2000', '4096'} [] 'logical'}), ...
'ColumnEditable',true, ...
'Data',myData);
uit.ColumnName = {'Fs','Sec','Amp','Answer',Summary','MultiFreq'};
set(uit, 'CellEditCallback', 'tdata = get(uit,''Data'')');
As mentioned, the script allows data entry and changes, and captures those data in tdata. When in a function the following error is reported in the Command Window when data in the uitable is changed:
Error using matlab.ui.internal.controller.uitable.WebMWTableController/fireCallbacksFromCellEdit (line 719)
Error while evaluating Table CellEditCallback.
If I add a waitfor(fig) ahead of the set function i see this error:
Error usingmatlab.ui.comtrol.Table/set
Invalid or deleted object.
Error in GetWaveStructure2>GetWaveStructure3 (line 19)
set(uit, 'CellEditCallback', 'tdata = get(uit,''Data'')');
GetWaveStructure2>GetWaveStructure3, Get…3 is the function name I used when wrapping the script in a function. It appears the function is completing before the callback completes and the callback doesn't see the uit or Data.
Any suggestions on how to write the function properly?

Best Answer

This code now works. I added a callback function within the function itself and used waitfor(fig).
[tdata] = myfunction
tdata = {};
fig = uifigure('Position',[404 390 600 420]);
%

myData = {48000 30 10.6 'Choose','Enter Filename','false'};
%
uit = uitable('Parent', fig, ...
'Position', [20 350 550 60], ...
'ColumnFormat',({[] [] [] {'2000', '4096'} [] 'logical'}), ...
'ColumnEditable',true, ...
'CellEditCallback',@updatetable,...
'Data',myData);
uit.ColumnName = {'Fs','Sec','Amp','Answer','Summary','MultiFreq'};
function updatetable(hObject,callbackdata)
tdata = hObject.Data;
end
waitfor(fig)
end