How to skip a parameter in a function? ex: A = fread(obj,size,'precision') without size.
147 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
David
am 12 Dez. 2014
Bearbeitet: Thorsten
am 19 Dez. 2014
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 Kommentare
Akzeptierte Antwort
Guillaume
am 15 Dez. 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 Kommentar
Stephen23
am 18 Dez. 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.
Weitere Antworten (2)
Thorsten
am 18 Dez. 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 Kommentare
Greg
am 12 Dez. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!