How to Create Libraries in Matlab?
136 views (last 30 days)
OK, I switch gears often between C, Matlab, and Python-so I am really a beginner in the Matlab world. Is there any way, in pure Matlab to create the equivalent of a library? I have several closely related functions that control a piece of hardware. The individual functions are relatively simple, so it seems asinine to have to put each one in a separate m-file. Is there a way I can group them all together? I guess this would be the equivalent of creating a toolbox, but what reading I've done seems to recommend against this approach. In the separate file sceneraio--does Matlab cache the functions in any way, or does the file have to be read from disk every time I call the function? It would seem this would introduce lots of latency.
Andrew Newell on 24 Mar 2015
Edited: Andrew Newell on 24 Mar 2015
The simplest approach would be to put the functions in their own folder and add that folder to the MATLAB path. Whether it's asinine to put each function in a separate file may be a matter of taste. If they are in separate files, you can see them all at a glance, and you can create a contents report, which you can then organize in any way that seems useful.
More Answers (2)
Jeff Miller on 22 Feb 2018
Another option is to combine the functions into a single class, making them static functions so that you don't need to instantiate the class. Something like this:
function y = myfunc1(x)
function y = myfunc2(x)
end % static methods
end % classdef
Assuming this classdef file is on your path, you can then call the individual functions from elsewhere using something like:
y = HardwareControllers.myfunc1(23);
dpb on 24 Mar 2015
Well, Matlab is what Matlab is, and separate m-files are the way functions are implemented in Matlab. So, the simple answer is what seems different from the perspective of another language implementation isn't 'asinine', just different.
There is one way that you can help and that is if there are a limited number of functions that are user-callable but they rely on helper functions to do their work, then those helper functions can reside in the same file as the routines that call them, but they are not visible from outside that m-file so they must truly be not needed except within that limited scope.
On performance, Matlab does cache the toolbox folders for minimizing the required load time. I'm not certain (and TMW doesn't document) just what the JIT compiler keeps in memory. But, in general, Matlab is a rapid development environment, not so much a runtime performance one so in general one simply codes what you need and sees if can live with the performance albeit there are certainly "tricks" to getting what performance there is. Using preallocation and vectorization are two of the most obvious but also the most rewarding when done right. Computationally, if one can get the needs down to BLAS or similarly compiled functions, in the limit one can get those computational speeds approaching a strictly compiled function. OTOH, if one makes extensive use of the higher-level data abstractions and there are many m-file implementations and not much can be vectorized, performance can (and will) lag behind what might possibly be done directly in a compiled language. Then again, one can likely implement the function far more quickly than write that application, too...