MATLAB Creating Conditional Callback Function in Matlab GUI

  • Thread starter Thread starter calt3ch
  • Start date Start date
  • Tags Tags
    Gui Matlab
Click For Summary
The discussion centers on the use of callback functions in programming, specifically in the context of button click events. The original poster seeks a solution for executing the same function when different buttons are clicked, particularly when certain conditions are not met. Instead of duplicating code for each button, the suggestion is to refactor the code by creating a separate function that encapsulates the shared functionality. This approach promotes better coding practices, making the code easier to follow and maintain. The concept of procedural programming is mentioned as a common solution to this issue, emphasizing the importance of clean and efficient code structure. The original poster acknowledges the value of this advice and expresses intent to implement it.
calt3ch
Messages
4
Reaction score
0
So I am a bit unfamiliar with Callback functions etc. But here is what I want:

when someone clicks on a button, something happens. Well I want it so that when they click on another button and a certain condition isn't met, then the same function carried out by clicking a completely different button is carried out. I mean I guess I could just copy and paste the code so that it appears twice, but I feel like there's an easier way that involves just calling the functionnamehere_Callback function. Or am i crazy?
 
Physics news on Phys.org
This is a sign that you're not coding with good style. "Good" defined here as easy to follow and maintain.

Break the button handler function up into separate functions for each thing it does, and call that function from both button handler function.
Example:

Old:
Code:
button1()
{
    // do stuff
}

button2()
{
    // do same stuff
}

New:
Code:
button1()
{
    do_stuff()
}

button2()
{
    do_suff()
}

do_stuff()
{
    // do stuff
}

This is called procedural programming, and it has it's own issues, but it's the common solution to your problem.
 
Yes of course. I was thinking of doing that before, but tbh I wasnt putting a whole lot of thought into the GUI code; it was kind of an unnecessary part of my project... but yes, good idea i will do that ultimately i guess :)
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 2 ·
Replies
2
Views
16K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
4K