Creating Conditional Callback Function in Matlab GUI

  • Context: MATLAB 
  • Thread starter Thread starter calt3ch
  • Start date Start date
  • Tags Tags
    Gui Matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
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 :)