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

49 visualizaciones (últimos 30 días)
Alvaro Cruz-Cabrera
Alvaro Cruz-Cabrera el 10 de Jul. de 2020
Comentada: Stephen23 el 11 de Jul. de 2020
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 comentario
Stephen23
Stephen23 el 11 de Jul. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 10 de Jul. de 2020

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by