I can't retrieve the user input information in a table when the user closes the table.
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Adam Danz
2020-3-9
编辑:Adam Danz
2020-3-9
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
更多回答(1 个)
Walter Roberson
2020-3-9
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 个评论
Adam Danz
2020-3-10
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!