How to Create Libraries in Matlab?
Show older comments
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.
2 Comments
Steven Lord
on 22 Feb 2018
If MyFunction1, MyFunction2, etc. are local functions in the file MyLibrary.m they will not be in scope outside of the MyLibrary file itself and cannot be called by code outside that file. See this page for a brief description of how the main function in a file and local functions in that file differ.
Chris Knab
on 23 Feb 2018
Steven,
My mistake, I deleted my comment. Please feel free to remove this. I tested this on functions that were no longer in my program. I was in the middle of debugging something else, therefore in a very 'transient' state of programming. :) Sorry to any and all for the confusion and bad information.
Sincerely, Chris
Accepted Answer
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:
classdef HardwareControllers
methods (Static)
function y = myfunc1(x)
end
function y = myfunc2(x)
end
% etc
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);
3 Comments
Walter Roberson
on 23 Feb 2018
I would suggest that instead of this, it would make more sense to create a "package" as described at https://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html
Jeff Miller
on 23 Feb 2018
Do packages allow multiple functions to reside in the same m file (yet be independently callable from outside that m file)? I thought this was what the OP was trying to achieve.
Walter Roberson
on 23 Feb 2018
Ah, good question, I forgot that was what was being asked.
dpb
on 24 Mar 2015
1 vote
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...
2 Comments
jacob davis
on 28 Mar 2017
a = arduino('COM6','Uno','Libraries','ShiftRegister'); _ when i am creating the arduino object its showing the error like this what i have to do for solving this___
Invalid value 'ShiftRegister' for Libraries. Valid libraries are 'Adafruit/MotorShieldV2, I2C, SPI, Servo'.
Chandra Bhanu Solanki
on 28 Mar 2017
Yeah you cannot do this as shift register is not a library in matlab, if you want to create a library than there is a well procedure to do so.
Categories
Find more on Performance and Memory in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!