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
댓글 수: 20
추가 답변 (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에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!