subset of varargin as function input
6 views (last 30 days)
Show older comments
I call a function in the main code
plotFT( var1,var2,...,var13)
whose input variable can be either 10 or 13. The function in subject is defined as follows:
function plotFT( varargin )
switch nargin
case 10
fun1( varargin{1:10} ) %why doesn't work just with varargin as input?
case 13
subplot(1,2,1)
fun1( varargin{1:10} )
subplot(1,2,2)
fun2( [ varargin{1:3} varargin{11:13} ] ); %I can't't catch the mistake
otherwise
warning('Unexpected number of inputs')
end
fun1 works but for fun2 the followinf error message is prompted: "Error using horzcat. The following error occurred converting from char to struct: Conversion to struct from char is not possible." Questin1:
0 Comments
Accepted Answer
Stephen23
on 17 Sep 2018
Edited: Stephen23
on 17 Sep 2018
Try this:
function plotFT(varargin)
switch nargin
case 10
fun1(varargin{:})
case 13
subplot(1,2,1)
fun1(varargin{1:10})
subplot(1,2,2)
fun2(varargin{[1:3,11:13]});
otherwise
warning('Unexpected number of inputs')
end
"why doesn't work just with varargin as input?"
Because varargin is one cell array. If fun1 expects ten separate inputs, then you cannot just pass it one cell array. The syntax cellarray{...} generates a comma-separated list, which is equivalent to writing each cell as its own input argument, like this:
fun1(varargin{1}, varargin{2}, ... )
To learn more about comma-separated lists read this explanation:
"I can't't catch the mistake"
Because you try to concatenate the contents of varargin together, which clearly does not make sense for your input arrays, because they are different classes/sizes/...
After reading the links I gave you, you will know that your code
[ varargin{1:3} varargin{11:13} ]
is exactly equivalent to this
[varargin{1},varargin{2},varargin{3},varargin{11},varargin{12},varargin{13}]
Is there much point in concatenating all of them together? I doubt it. Most likely you want them as separate inputs, which, after reading the links I gave you, you will know that you can achieve using either of these two syntaxes:
fun2(varargin{[1:3,11:13]})
fun2(varargin{1:3},varargin{11:13})
both of which are equivalent to writing
fun2(varargin{1},varargin{2},varargin{3},varargin{11},varargin{12},varargin{13})
which I suspect is what you were trying to achieve (although you do not actually specify anything about fun2 in your question, such as how many inputs it requires, so I had to guess).
3 Comments
Stephen23
on 18 Sep 2018
Edited: Stephen23
on 18 Sep 2018
@drSlump: do whatever is clearest for your code. A structure, as Steven Lord suggested, might be a good idea. Ten inputs might be okay. I don't think using one vector would be an improvement, as having individual inputs/structure fields makes the purpose, documentation, and checking clear, if a little verbose. Using one cell array requires processing of the cell array to check/count/.. its contents.
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!