Declare function for specific matlab versions

vecnorm was introduced in R2017b. To ensure compatibility with previous versions I'd like to conditionally declare my own vecnorm function, based on the Matlab version, e.g. only declare it for versions less than R2017b.
Ideally, I'd like to call the function with its actual name, e.g. vecnorm. Also, I would like to not have to make any modifications at the site of the caller.
How can I achieve this in Matlab?

回答 (1 件)

Guillaume
Guillaume 2018 年 10 月 3 日

0 投票

You have several options
Option 1 is to use verLessThan. Have your code call a stub vecnormstub function in which you perform the version check, if version is R2017b or later the stub calls vecnorm otherwise it calls your own implementation (which would have a different name). The main issue with this approach is finding the actual version numbers to give to verLessThan since Mathworks doesn't actually document them.
Option 2 is to use try catch. Have your code wrap the call to vecnorm into a try catch. If the catch triggers, and the exception is MATLAB:UndefinedFunction, call your own implementation (which again would have a different name). If the exception is not MATLAB:UndefinedFunction, rethrow
Option 3, if on <R2017b you want your implementation to be available even outside of your own code, is to put your function in a folder that you only add to the path if the version is <R2017b (again check with verLessThan).

カテゴリ

ヘルプ センター および File ExchangeLinear Algebra についてさらに検索

製品

リリース

R2017b

質問済み:

2018 年 10 月 3 日

編集済み:

2018 年 10 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by