I can't retrieve the user input information in a table when the user closes the table.

2 views (last 30 days)
Hello,
I am trying to collect the information of a popup uitable in MATLAB GUI with the DeleteFcn property, but it keeps giving me this error:
Unrecognized function or variable 'MyDeleteFcn'.
f = figure('Name','Info.','NumberTitle','off');
set(gcf, 'Units', 'Inches', 'Position', [7, 3, 3.8, 6], 'PaperUnits', 'Inches', 'PaperSize', [16, 9.0]);
t = uitable(f,'Position',[10 5 400 565],'ColumnWidth',{150,150},'ColumnEditable',[false true], 'DeleteFcn','data = MyDeleteFcn(gcbo)');
set(t,'Data',[matrix1, matrix2]); % Use the set command to change the uitable properties.
set(t,'ColumnName',{'';''});
uiwait
However, I defined this function earlier in the GUI
function data = MyDeleteFcn(t)
data = get(t,'Data');
Could anyone tell me what is it that I'm doing wrong?
My goal is to collect the information of a popup uicontrol as soon as the user closes the table in a GUI.
Any help is much appreciated.

Accepted Answer

Adam Danz
Adam Danz on 9 Mar 2020
Edited: Adam Danz on 9 Mar 2020
Callback functions do not return outputs. You must store the values somewhere and then retreive them. Here's a solution to this problem:
Step 1: Pass the GUI handle in to the delete function
mainGUIhandle is a handle to the main GUI figure.
% Create the UITable and its parent figure
f = figure('Name','Info.','NumberTitle','off');
t = uitable(f,'DeleteFcn', {@MyDeleteFcn, mainGUIhandle}); % mainGUIhandle is handle to main GUI
set(t,'Data',rand(10,3));
Step 2: Define delete function, store UITable data in main GUI
function data = MyDeleteFcn(hObj, event, GUIhandle)
data = hObj.Data; % Get data from UI table
dat = guidata(GUIhandle); % Get data stored in main GUI (if any)
dat.data = data; % Store the UI table data
guidata(GUIhandle, dat) % Load the data into the main GUI
Step 3: Retrieve data from any callback function that has access to the main GUI handle
mainGUIhandle is a handle to the main GUI figure.
% From any callback function that has access to the main GUI figure handle
dat = guidata(mainGUIhandle);
dat.data % This is the UITable data
  20 Comments
Feri
Feri on 10 Mar 2020
I found the issue, I didn't follow your step 3 completely. Slow learner.
Thanks a million.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 9 Mar 2020
When you use a quoted string to define a callback, the statement is evaluated inside the base workspace. It can only access functions that are defined as the first function inside of a .m file because it is not evaluated within the scope of your gui.
Adam has given you some good advice.
There is a hack, though: you can use
assignin('base', 'MyDeleteFcn', @MyDeleteFcn)
Then when the callback executes within the base workspace, the MyDeleteFcn that is seen in the base workspace is the function handle to the internal function.
The variable assigned to in the callback string will go into the base workspace.
  2 Comments
Feri
Feri on 9 Mar 2020
Thank you.
I should have everything inside the GUI, would you be able to help me with how to edit my code, so that the table information is saved in Swith Case 'Yes' when the user closes the table?
Thanks a million.
Adam Danz
Adam Danz on 10 Mar 2020
I didn't know about this, Walter. Thanks! What advantage is there for using this syntax? It seems limited and even difficult to debug, unless I'm not understanding it correctly. I'll have to tinker with it some time.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!