Can I give input arguments while referencing a callback function?
44 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Soumyatha Gavvala
le 20 Juin 2016
Commenté : Soumyatha Gavvala
le 20 Juin 2016
I'm new to object oriented MATLAB so bear with me.
I am plotting some data from my class S, as
Plot(s.X{i},s.Y{i},s.Data{i},...
set(gcf,'Name','Data1');
filepath = s.DirPath{1,i});
set(gca,'ButtonDownFcn',@mycallback);
Now I want to give this 'filepath' as one of the input argument for my callback function. But when I try to use filepath, it doesn't recognize it.
I also tried
set(gcf,'Tag',s.DirPath{1,i});
set(gca,'ButtonDownFcn',@mycallback);
and
get('Tag');
within my callback function.
Any help?
0 commentaires
Réponse acceptée
Geoff Hayes
le 20 Juin 2016
Soumyatha - I don't think that it is a good idea to overwrite the Tag property of your figure with
set(gcf,'Tag',s.DirPath{1,i});
This would (to my understanding) change the name of the figure and would make it difficult to reference if you needed to elsewhere in the app.
So to pass in this file path parameter, you would do something like
set(gca,'ButtonDownFcn',{@mycallback, filepath});
and your callback signature would become
function mycallback(source, eventdata, filepath)
This should work, but you have to remember that the filepath will always have the value that was set to this variable when the callback was assigned. So if this variable ever gets modified, the callback will not see the change.
An alternative, is to nest your callback within the parent function which allows the nested function to access the variables defined within its parent. For example,
function myMainFunc
% some code here
Plot(s.X{i},s.Y{i},s.Data{i},...
set(gcf,'Name','Data1');
filepath = s.DirPath{1,i});
set(gca,'ButtonDownFcn',@mycallback);
function mycallback(source, eventdata)
% some code here
% use filepath
save(filepath,....);
end
end
Try nesting your callback. That way, if filepath is changed by some other callback, then mycallback will be able to access the changed variable.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Specifying Target for Graphics Output dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!