How to access app designer GUI pushbutton callback inside my function?

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 Comments

@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?
@Geoff Hayes Thank you.
Please see snapshot below.
My custom function defined within a seperate *.m file.
Is it clearer now?

Sign in to comment.

Answers (1)

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 Comments

@Rik Thank you.
what do you mean? Please see snapshot in my reply to @Geoff Hayes.
My custom function is indeed in a seperate file.
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.
@Rik If I understand your suggestion you're saying, under "takeLeft" push button callback there should be only a single line.
A calling to some custom function, my_func_2.
Then, instead of calling to "takeLeft" callback, I could simply call for my_func_2...?
In prinicple that might work, but it's a bit of eradicating the idea of App Designer where I can add dedicated callback functions for my objects. I mean, literally writing lines of code there, and not only calling for other seperate file functions.
Why, if I'm inside App Designer Code View, then I can call from one button callback to other buttons callbacks in my GUI. But, if I'm inside a function, suddenly those callbacks are not available?
I'd expect to have something like:
app.takeLeft_pushBtnButtonPushed
to work. If "app" can take variables inside the function, why can't it take also the callbacks?
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
That's the error I got when tried your proposal:
K>> app.takeLeft_pushBtnButtonPushed(app,[]);
Cannot access method 'takeLeft_pushBtnButtonPushed' in class 'mainRunner_GUI'.
'mainRunner_GUI' is the name of my *.mlapp file.
perhaps there is a way to somehow pass the method 'takeLeft_pushBtnButtonPushed' outside of the class 'mainRunner_GUI'? so that it could be used inside my custom function as well?
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.
according to snapshot below --> takeLeft_pushBtn is already "public", isn't it?
Can you advice please how can I make the method public?
I like using App Designer (and previosuly GUIDE) because of the ease of "drag & drop" for components creation. Don't like to define all the positions, by myself. Always a headache with setting it right... and then if you want to re-arrange your objects, it's a nightmare...
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.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Asked:

on 8 Mar 2022

Commented:

Rik
on 10 Mar 2022

Community Treasure Hunt

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

Start Hunting!