Azzera filtri
Azzera filtri

Can I include functions from another .m-file in my application?

12 visualizzazioni (ultimi 30 giorni)
Hi I'm making an application with the GUIDE-tool and the local callbacks are beginning to add upp to a pretty long file. I would like to group functions together and put them in a separeate file or at least hide all of them at once. Can this be done? Some programming languages lets you put include-statements in the beginning of the file. I'd like something like this in matlab.
  4 Commenti
Adam
Adam il 31 Ago 2016
If they literally all do the same thing you know you can point multiple editbox callbacks at the same function I assume? You don't have to have a 1-1 mapping between edit boxes and their callbacks, you just need to edit the 'Callback' property of all edit boxes to be the same function.
Stephen23
Stephen23 il 31 Ago 2016
Modificato: Stephen23 il 31 Ago 2016
@Fredrik Zachrisson: "Having all these callback functions that all do the same thing with different parameters in the same file gets a bit cumbersome."
So don't have lots of callback functions: just write one function, and call that from every uicontrol. You can even call it with some parameters or other arguments, however you need to. This will likely be much simpler to write and maintain than replicating the same code into lots of functions.
Also read about private functions.

Accedi per commentare.

Risposte (1)

Adam
Adam il 31 Ago 2016
Modificato: Adam il 31 Ago 2016
You can include functions from any file that is on your path. Only 1 externally accessible function per file though if you aren't using a class. No need for include files, they will be found if they are on the path.
  2 Commenti
Fredrik Zachrisson
Fredrik Zachrisson il 31 Ago 2016
Thanks but that would mean I get as many new files as I have functions right now and the problem is that the functions are too many. I don't think my workflow would improve very much. I will have a look at classes.
Adam
Adam il 31 Ago 2016
1 function per file can be annoying sometimes, but you tend to get use to it, especially if you just put them all in one folder or even a package folder. It is neater than putting them all in the GUIDE file and especially, of course, if you want re-usability of any of the functions outside of the GUIDE application.
Personally I do 90% of my programming in classes nowadays, but they do have a learning curve. Less so if you just do the basics with them though.

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by