Is there a way to use my own function file for a Matlab function?

3 Ansichten (letzte 30 Tage)
Dear Matlab users,
I would like to know if it is possible to substitute a Matlab function with my own .m file? Why I want do to this: I process a lot of imaging data and I seem to run out of Image_Toolbox licenses sooner than Matlab licenses. The processing pipeline seems to use only `imerode` function from the toolbox, so I wrote my own function `own_imerode.m`. Luckily, it is quite straight-forward. However, I would like not to replace `imerode` with `own_imerode` throughout the code for two reasons - backward compatibility and hassle of going through the code. Instead, I was wondering if it is possible to tell Matlab to somehow use `own_imerode` when `imerode` is called, like an alias in a bash shell? I guess, one way would be to call my function also `imerode` and place the path to it on top of the list. Is there another way?
Best, Renat.

Akzeptierte Antwort

Andreas Goser
Andreas Goser am 12 Jan. 2018
Renat, the root cause appears to be a lack of purchased toolboxes. Also you seem to process a lot of images. So I suggest you also look for accessing a cluster. You might have a MATLAB Distributed Computing Server license at your institution.
Beside that, I would not fiddle with the installation. You may want to a simple IF THEN ELSE statement at the beginning that looks for the toolbox being available and if yes use it and if not the alternative way. Command to use could be:
license('test','Image_Toolbox')
  1 Kommentar
Renat
Renat am 12 Jan. 2018
Bearbeitet: Renat am 12 Jan. 2018
I have access to an HPC cluster and this where I tun into the problem. It looks like after New Year the number of IPT licenses halved. So I looked into the processing pipeline code and realized that we don't really need IPT and I can use `conv2` instead.
I like the idea of checking for an available license and use my version if there is none. Thank you for the suggestion, I think I will use that!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 11 Jan. 2018
The defined route is to use the same function name and have the function earlier on your search path.
You can reduce the impact on other code by putting your imerode into a directory named private under the directory that calls the function. https://www.mathworks.com/help/matlab/matlab_prog/private-functions.html

John D'Errico
John D'Errico am 11 Jan. 2018
Bearbeitet: John D'Errico am 11 Jan. 2018
That is exactly how to solve the problem - just put it in a directory on your search path above the IPT directory.
Note that this also effectively disables the IPT imerode function, so if you ever need to use any capabilities of that tool that are not in your own code, it is no longer available to you. To restore access to the old imerode, you would need to change the search path, or delete or rename your version.
You may get warning messages from MATLAB, that your function steps on existing code. At least when you use it initially, but that warning seems to go away after the first use.
function chol(A)
disp('The sky is falling')
end
chol(2)
The sky is falling
One other way to temporarily recover use of the original tool is to use builtin, although I think that will only apply to actual builtin tools like chol. Toolbox functions seem not to be recognized by builtin.
L = builtin('chol',2)
L =
1.4142
  1 Kommentar
Renat
Renat am 12 Jan. 2018
The code uses the IPT imerode function to do an erosion using a line strel. This is very easily replaced with `conv2` function. I dont need its other capabilities.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by