When you type in a Matlab function name and open a brace (e.g. sum(
) in the command window, a tooltip opens that shows all possible usages of this function.
When I write my own function that accepts varargin
, the tooltip shows just one option, which instead of varargin
puts ...
(e.g. myfunc(x,...)
).
How can I suggest more specific usages of my function to the user?
I can get you there without editing 'Syntax' that works on current releases, though a little muddy.
The below function takes a range of required, optional, and optional name-value pairs for inputs. I want tool tip to give the user information on the required and optional inputs. varargin always exists even if no variables are passed into it, and inputParser works pretty well when handed varargin. I'm going to take advantage of this by leaving my optional inputs on the function call, but then stack them into varagin before I ever work with them.
tool tip will display myFun( frequency, gain_optional, phase_optional, ...)
function myFun( frequency, gain_optional, phase_optional, varargin )
%% Get mandatory and optional Inputs using inputParser
p = inputParser;
% mandatory inputs
argName = 'frequency';
validation = @(x) validateattributes( x, {'numeric'}, {'2d','positive'} );
p.addRequired( argName, validation );
% optional inputs
% start by packaging all optional inputs into varargin. The two optional
% inputs were kept out of varargin so they will display in the tooltip
if nargin > 2
varargin = [{phase_optional} , varargin];
end
if nargin > 1
varargin = [{gain_optional} , varargin];
end
% add optional inputs into inputParser
argName = 'gainLinear';
default = [];
validation = @(x) validateattributes( x, {'numeric'}, {'2d','positive'} );
p.addOptional( argName, default, validation );
argName = 'phaseDeg';
default = [];
validation = @(x) validateattributes( x, {'numeric'}, {'2d','positive'} );
p.addOptional( argName, default, validation );
% name-value pairs
argName = 'model';
default = [];
validation = @(x) validateattributes( x, {'numeric'}, {'vector','positive'} );
p.addParameter( argName, default, validation );
argName = 'order';
default = [];
validation = @(x) validateattributes( x, {'numeric'}, {'scalar','integer','>',0,'<',3} );
p.addParameter( argName, default, validation );
% pull out all passed data with the inputParser and stash into input struct
p.parse(frequency, varargin{:});
input = p.Results
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With