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

조회 수: 9 (최근 30일)
Fredrik Zachrisson
Fredrik Zachrisson 2016년 8월 31일
편집: Stephen23 2016년 8월 31일
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
Adam
Adam 2016년 8월 31일
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 2016년 8월 31일
편집: Stephen23 2016년 8월 31일
@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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam
Adam 2016년 8월 31일
편집: Adam 2016년 8월 31일
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
Fredrik Zachrisson
Fredrik Zachrisson 2016년 8월 31일
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 2016년 8월 31일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by