Effacer les filtres
Effacer les filtres

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

54 vues (au cours des 30 derniers jours)
Alvaro Cruz-Cabrera
Alvaro Cruz-Cabrera le 10 Juil 2020
Commenté : Stephen23 le 11 Juil 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 commentaire
Stephen23
Stephen23 le 11 Juil 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.

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 10 Juil 2020

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by