필터 지우기
필터 지우기

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

조회 수: 145 (최근 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