subset of varargin as function input

8 vues (au cours des 30 derniers jours)
Luigi
Luigi le 17 Sep 2018
Modifié(e) : Stephen23 le 18 Sep 2018
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:

Réponse acceptée

Stephen23
Stephen23 le 17 Sep 2018
Modifié(e) : Stephen23 le 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 commentaires
Luigi
Luigi le 18 Sep 2018
Thanks for the answer. The two functions have been defined as follows:
fun1( var1,var2,...,var10 )
...
end
fun2( var1,var2,var3,var11,var12,var13 )
...
end
so the function fun2 is called only if nargin = 13 in plotFT. So the problem is that I have defined the input of the functions fun1 and fun2 as separate variables and then I have to specify the number of input input variable, is it right? Is that a good practice? Or could/should I define the input of fun1 and fun2 as a vector (?), e.g.
fun2( var_vett )
var1 = var_vett(1);
var2 = var_vett(2): %and so on
...
end
Stephen23
Stephen23 le 18 Sep 2018
Modifié(e) : Stephen23 le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by