How to skip a parameter in a function? ex: A = fread(obj,​size,'prec​ision') without size.

156 次查看(过去 30 天)
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...

采纳的回答

Guillaume
Guillaume 2014-12-15
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 个评论
Stephen23
Stephen23 2014-12-18
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.

请先登录,再进行评论。

更多回答(2 个)

Thorsten
Thorsten 2014-12-18
Supply an empty argument; the function should use the default instead:
fread(fid, [], 'uint8')
(for fread you can just drop the second argument)
  2 个评论
David
David 2014-12-19
EDU>> fread(s, [], '*uint16');
Error using serial/fread (line 171)
Invalid PRECISION specified. Type 'help serial/fread' for more information.
Thorsten
Thorsten 2014-12-19
编辑:Thorsten 2014-12-19
Too bad it does not work for fread. But for other functions I've used this syntax. It works for those functions where the default value is used if the number of arguments is to small OR the argument is empty.

请先登录,再进行评论。


Greg
Greg 2014-12-12
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 个评论
Greg
Greg 2014-12-12
编辑:Greg 2014-12-12
Not that I know of. Usually I edit the order of the inputs in the function code if I need to do this, but I see that's not possible for fread. I agree it would be nice to have a simple solution to this.
David
David 2014-12-12
Oh, I did not think about that. Then I know fread is no need to try but I will certainly try to change the order in others. Thanks.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by