Passing a variable from a call back function - matlab 2020a
54 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 11 Jul 2020
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.
You should read this excellent tutorial: https://www.mathworks.com/matlabcentral/answers/483657-how-to-create-a-gui
Answers (1)
See Also
Categories
Find more on Migrate GUIDE Apps 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!