Passing a variable from a call back function - matlab 2020a

84 次查看(过去 30 天)
I am trying to pass two variables back from a call back, but I am being dense an not being able to do it.
Right now, I am using global variable to execute that part but this is not optimal and no good coding technique.
What I am trying to do is get a cell location from an uitable that I create in the main function:
numx= readtable(FullPath); %this is the table
fig = uifigure;
uit= uitable(fig,'Data',numx); %I create a uitable
global Fiducial %here I got the global variable where I get the location of the cell
Fiducial= [0,0]; % I set it to zero so I don't move in my code until I select a cell
set(uit, 'CellSelectionCallback', {@TableSelectionCB}); %Here is the call
while (Fiducial(1)+ Fiducial(2)== 0) % wait until I get my cell
pause(0.0001)
end
This is the callback function:
function [Fiducial]= TableSelectionCB(hobject, eventdata)
global Fiducial
if isempty(eventdata.Indices)
return
end
hobject.UserData= eventdata.Indices;
Fiducial= eventdata.Indices;
end
I think I am doing a couple things wrong, but I want to really pass that cell location to the Fiducial variable on the main code without having to declare it as a global variable.
Thanks
Alvaro
  1 个评论
Stephen23
Stephen23 2020-7-11
Callbacks are not called with any outputs. Although you can certainly write the callbacb function with output arguments, they will be ignored when the callback is triggered.
The MATLAB documentation explains how to pass data between callback workspaces:
The best method to use depends on how you are creating the GUI: e.g. for GUIDE use the handles structure, for App Designer assign to the object itself. If you are sensibly writing your own GUI code then I recommend using nested functions to pass data around the GUI.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2020-7-10

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by