MATLAB Answers

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

16 views (last 30 days)
Alvaro Cruz-Cabrera
Alvaro Cruz-Cabrera on 10 Jul 2020 at 22:28
Commented: Stephen Cobeldick on 11 Jul 2020 at 4:50
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

Stephen Cobeldick
Stephen Cobeldick on 11 Jul 2020 at 4:50
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.

Sign in to comment.

Products


Release

R2020a