フィルターのクリア

Overloading User Written Function

3 ビュー (過去 30 日間)
Marc
Marc 2014 年 8 月 8 日
コメント済み: Marc 2014 年 8 月 8 日
I have a function that can take a variable number of inputs. The function definition looks like this:
function [i] = fun(func,argin1,argin2,argin3, argin4, argin5,argin6,argin7)
These variables then get passed into a switch statement like this:
switch nargin
case 1
try
i = calllib('User_DLL',func);
catch exception
str = {exception.identifier,sprintf('%s did not load',func)};
h = warndlg(str);
waitfor(h);
end
case 2
try
i = calllib('User_DLL',func,argin1);
catch exception
str = {exception.identifier,sprintf('%s did not load',func)};
h = warndlg(str);
waitfor(h);
end...
And it keeps going for all possible number of inputs. Is there a way to overload this function so that I just need to know the number of input and then I can pass the input arguments into calllib in the order they first appeared in? It seems like this process can be greatly streamlined.

採用された回答

Adam
Adam 2014 年 8 月 8 日
編集済み: Adam 2014 年 8 月 8 日
I think generally for this you would use a varargin type of input to your function.
You can then test the length of varargin and you can pass it on as varagin{:} to another function that also wants those arguments in the same way.
e.g. if you have a function that will call the plot(...) function you can have a varargin argument to your function (even after named arguments) and then just pass it straight to plot as varargin{:} if you know the arguments are in a form that plot accepts - e.g. property, value pairs.
For example I have a function in one of my classes that does the following:
function setGUIHandleProperty( guiHandle, varargin )
if ishandle( guiHandle )
set( guiHandle, varargin{:} );
end
end
which just acts as a wrapper for setting some figure properties.
  1 件のコメント
Marc
Marc 2014 年 8 月 8 日
Thank you. That's exactly what I was looking for, trimming down an 85 line long switch statement to 10 lines.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by