0

I'm currently working on a MATLAB GUI program. I'm trying to organize the program such that I have a single .m file that contains the GUI logic (responding to button presses and updating displays) and then another .m file that contains functions for doing the actual number crunching. Essentially, I'm trying to make the program more readable by separating the two components.

However, I'm running into a problem. When I attempt to define a function in a .m file, it states that the function name and file name must match. However, I want this .m file to contain several functions that can be called by the GUI .m file. This would thus force me to have a separate .m file for every function that will be called by the GUI .m file... Thus requiring a mess of .m files.

So my question: Is it possible to have a single .m file contain several functions that are callable by an external .m file? Imagine I have a script called math.m. It would be nice to have that file define functions such as add, subtract, divide, multiply, etc. defined in a single file (as opposed to being defined across several files).

Izzo
  • 4,461
  • 13
  • 45
  • 82
  • When I last used MATLAB the answer was "you can't." My solution was to have a single entrance point for the .m file and pass a string which selects which private function in the .m file to call. – Cort Ammon Sep 09 '16 at 00:30
  • Hmm.. interesting. I suppose that would work. That brings up another question: does MATLAB feature any kind of #define or enums such that you can pass values efficiently into a function? I'd imagine there might be a performance hit when trying to compare strings. – Izzo Sep 09 '16 at 00:33
  • I've never seen anything like that. Of course, I have strong opinions about MATLAB and efficient code execution, especially since Python has matplotlib now. I haven't been exactly scouring the documentation for ways to make it fast ;-) – Cort Ammon Sep 09 '16 at 00:40
  • @CortAmmon Haha, I don't blame you. MATLAB's got all these nice DSP libs that I just can't seem to get away from. I might as well try to read up on some of the new features of the language. – Izzo Sep 09 '16 at 00:48
  • 1
    @Teague What is wrong with separate files for every function? It keeps your code clean, with short files. If you organise it in subfolders you are easy to go, especially with the ctrl+d functionality. Also, your version control system will like it more. – Bernhard Sep 09 '16 at 06:27
  • @Bernhard Hi Bernhard, that is the route I'm going. I suppose due to the fact that MATLAB will implicitly search folders/subfolders for function definitions, it's not that big of a deal. However, I was in the mindset of C and C++ where you would be required to have a #include for every single file (unless you have a fancy IDE or know your way around 'make' real well). – Izzo Sep 09 '16 at 14:12

0 Answers0