How to Create Libraries in Matlab?

조회 수: 196(최근 30일)
Jim
Jim 2015년 3월 24일
댓글: Chris Knab 2018년 2월 23일
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
Chris Knab
Chris Knab 2018년 2월 23일
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

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

채택된 답변

Andrew Newell
Andrew Newell 2015년 3월 24일
편집: Andrew Newell 2015년 3월 24일
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
Walter Roberson
Walter Roberson 2018년 2월 22일
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.

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

추가 답변(2개)

Jeff Miller
Jeff Miller 2018년 2월 22일
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
Walter Roberson
Walter Roberson 2018년 2월 23일
Ah, good question, I forgot that was what was being asked.

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


dpb
dpb 2015년 3월 24일
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
Chandra Bhanu Solanki
Chandra Bhanu Solanki 2017년 3월 28일
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.

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

범주

Find more on Simulink Environment Customization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by