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

148 views (last 30 days)
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...

Accepted Answer

Guillaume
Guillaume on 15 Dec 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 Comment
Stephen23
Stephen23 on 18 Dec 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.

Sign in to comment.

More Answers (2)

Thorsten
Thorsten on 18 Dec 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 Comments
David
David on 19 Dec 2014
EDU>> fread(s, [], '*uint16');
Error using serial/fread (line 171)
Invalid PRECISION specified. Type 'help serial/fread' for more information.
Thorsten
Thorsten on 19 Dec 2014
Edited: Thorsten on 19 Dec 2014
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.

Sign in to comment.


Greg
Greg on 12 Dec 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 Comments
Greg
Greg on 12 Dec 2014
Edited: Greg on 12 Dec 2014
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 on 12 Dec 2014
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.

Sign in to comment.

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!