Borrar filtros
Borrar filtros

How to Create Libraries in Matlab?

110 visualizaciones (últimos 30 días)
Jim el 24 de Mzo. de 2015
Comentada: Chris Knab el 23 de Feb. de 2018
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 comentarios
Steven Lord
Steven Lord el 22 de Feb. de 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
Chris Knab el 23 de Feb. de 2018
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

Iniciar sesión para comentar.

Respuesta aceptada

Andrew Newell
Andrew Newell el 24 de Mzo. de 2015
Editada: Andrew Newell el 24 de Mzo. de 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.
  4 comentarios
Caglar Kutlu
Caglar Kutlu el 13 de Oct. de 2017
So you work for Matlab?
Walter Roberson
Walter Roberson el 22 de Feb. de 2018
Caglar Kutlu: none of Andrew Newell, dpb, or Aaron Jean-Baptiste work for Mathworks (and I do not either.)
I used to be able to work with systems of interacting mostly-monolithic code, keeping all of the interactions for tens or hundreds of thousands of lines in my head, and doing what would today be called "refactoring" -- rewriting the interactions into smaller units with well-defined boundaries, imposing standards on variable names, working through the side effects of global variables, commenting, documenting, and so on. It was quite time-consuming work that took a lot of concentration. You have to spend a lot of time rebuilding the program just to do the same thing it already does (except with fewer bugs) before you can even think about extending the functionality. Very slow work, mostly thankless, and not many people can do it -- and even fewer are willing to do it. Unfortunately I can no longer handle that level of complexity.
I know from experience that it really is easier to work with shorter units of code with well-defined functionality, than with large files that include all of the various functions. You do, though, need a decent means to search multiple files, which is the main advantage of the large file that includes a lot of different routines.

Iniciar sesión para comentar.

Más respuestas (2)

Jeff Miller
Jeff Miller el 22 de Feb. de 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)
function y = myfunc2(x)
% 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 comentarios
Jeff Miller
Jeff Miller el 23 de Feb. de 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
Walter Roberson el 23 de Feb. de 2018
Ah, good question, I forgot that was what was being asked.

Iniciar sesión para comentar.

dpb el 24 de Mzo. de 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...
  2 comentarios
jacob davis
jacob davis el 28 de Mzo. de 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
Chandra Bhanu Solanki el 28 de Mzo. de 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.

Iniciar sesión para comentar.


Más información sobre MATLAB Compiler en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by