How to access app designer GUI pushbutton callback inside my function?
Afficher commentaires plus anciens
Hello,
I'm building a GUI via AppDesigner.
I have 2 push buttons: pushBtn_A & pushBtn_B.
pushBtn_A has its own "button pushed" callback.
pushBtn_B has its own "button pushed" callback.
I also have my own created function: my_func.
I'm trying to do the following:
inside pushBtn_B callback, I call for my function (my_func). And inside my_func, I'd like to call the pushBtn_A callback.
But once I'm inside my_func, it doesn't recognize pushBtn_A callback.
Error using mainRunner_GUI/pushBtnA_pushBtnButtonPushed
Cannot access method 'pushBtnA_pushBtnButtonPushed' in class 'mainRunner_GUI'.
How this can be solved?
Thanks!
2 commentaires
Geoff Hayes
le 8 Mar 2022
@Mark Golberg - is your function defined within the GUI or is it a separate file? Can you show the code for this function and how it expects to use the push button A callback function?
Mark Golberg
le 9 Mar 2022
Réponses (1)
Rik
le 8 Mar 2022
0 votes
My suggestion would be to move the code you want to call to a separate function. In my view, the callback of an object should only gather data from the user and pass those off to functions that do the actual work. If you want to call the same code from A as from B that is a clear sign that it is more general than a simple callback.
8 commentaires
Mark Golberg
le 9 Mar 2022
Rik
le 9 Mar 2022
It doesn't even need to be a separate file. Your takeLeft function does things. You want to do those same things in your Merge function. That means the contents of takeLeft should be moved to a separate function, since that is shared code. Then both takeLeft and Merge can call that function.
Mark Golberg
le 9 Mar 2022
Rik
le 9 Mar 2022
Yes, that is exactly what I mean.
I don't work with AppDesigner, so I can't answer your later questions. In principle there shouldn't be any issues creating a local function inside the same file. For general advice and examples for how to create a GUI, have look at this thread.
From how I understand it, an AppDesigner GUI is essentially a class, with all callbacks being functions. So maybe you could try this?
% call the method with itself as the first input and an empty event
app.takeLeft_pushBtnButtonPushed(app,[]);
The reason why you can't use the callback function directly is that every function has access to other functions inside the same file, but not to internal functions in other files.
%file 1:
function y=fun1(x)
y=fun1_internal(x);
end
function y=fun1_internal(x)
y=x;
end
%file 2:
function y=fun2(x)
y=fun1_internal(x);
% ^^ this will error
end
Mark Golberg
le 9 Mar 2022
Rik
le 9 Mar 2022
Apparently this method is private? You could try to see if there are ways to set the method to public.
I would strongly suggest using a separate function. In my view a callback should be unique to an object. The fact you want to reuse the entirety of the code means it isn't unique to one object.
In a normal class file you can also define other methods/functions. Why don't you try to put your external function in the class definition file itself?
Personally I'm not a fan of AppDesigner, as it closes down your GUI even more than GUIDE did. I personally prefer writing my own GUI from code. You can still use a class with a uifigure if you want, but then it'll be trivial to make methods public or insert functions into that file.
Mark Golberg
le 10 Mar 2022
Rik
le 10 Mar 2022
Your screenshot suggests the handle to the object itself is stored in a public property. The callback takeLeft_pushBtnButtonPushed is a method, not a property. I don't know if it is possible in AppDesigner to set a method to public. As I said, I don't work with AppDesigner myself, so I would have to do the same as you: look in the menus and google if I don't see anything https://xkcd.com/627/.
I understand the appeal of GUIDE/AppDesigner. I like them for rapid prototyping. Once you have determined the layout you can put those positions in code. For some situations I use code to determine the position based on the number of buttons I need to create. Once you are no longer prototyping, layout changes should be very rare, so the additional effort is not really relevant. Compare the time you've been struggling with this problem to the half hour it takes to change all the position arguments of your components.
Catégories
En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

