How to skip a parameter in a function? ex: A = fread(obj,size,'precision') without size.
156 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello.
Is here any syntax to not enter the second input to a function while entering the third?
I like to use A = fread(s, 'uint16');
But matlab complains that 'uint16' is not valid for size...
0 commentaires
Réponse acceptée
Guillaume
le 15 Déc 2014
In general, there is no way to skip parameters that are defined in the function signature. If it's not your code, you have no choice but to provide all the arguments before the one you want to supply.
If you're writing your own code and you want to make your function flexible so that the user only has to provide the arguments that he needs, you have two options:
- The matlab way:
Use name - value pair arguments in combination with varargout. There are many ways to write this, for example:
function out = myflexiblefunction(required, varargout)
par1 = defaultvalueforpar1;
par2 = defaultvalueforpar2;
for argidx = 2:2:nargin
switch varargout{argidx}
case 'Parameter1'
par1 = varargout{argidx+1};
case 'Parameter2'
par2 = varargout{argidx+2};
end
end
out = required + par1 - par2;
end
Usage:
out = myflexiblefunction(somevalue, 'Parameter2', someothervalue);
I greatly dislike this method as a) it involves a lot of parsing that the programming language should do for you, b) the user gets no hint as to the name of optional arguments (no tab completion) and has to look them up from the documentation or code.
- The function object way:
Use a class with properties for each optional argument:
classdef myflexiblefunction
properties
Parameter1 = defaultvalueforpar1
Parameter2 = defaultvalueforpar2
end
methods
function out = run(this, required)
out = required + this.par1 - this.par2;
end
end
end
Usage:
fn = myflexiblefunction;
fn.Parameter2 = someothervalue;
out = fn.run(somevalue);
You get tab completion, the argument name parsing is done for you and you can reuse the function object so you don't need to supply the optional arguments several times. The downside is that the object syntax may be unusual to some people.
1 commentaire
Stephen23
le 18 Déc 2014
Option Three: using a struct to pass the desired options. This is used in some MATLAB functions (e.g. ode solvers), but requires a similar amount of internal processing to the name-value options. The advantages are:
- a clear linking of fields and values.
- keeping all the options within one saveable/passable/adaptable variable.
Plus de réponses (2)
Thorsten
le 18 Déc 2014
Supply an empty argument; the function should use the default instead:
fread(fid, [], 'uint8')
(for fread you can just drop the second argument)
2 commentaires
Greg
le 12 Déc 2014
The syntax for fread is for the second input to be the number of elements to read. If you're asking the question for this case specifically, why not just include that as an input? If you want to read the whole file and don't know how many elements it has, I believe you can just use a 'very large number' and it will read the whole file.
e.g. A = fread(s,10^100,'uint16');
3 commentaires
Voir également
Catégories
En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!