How to supply perticular varargin entries of the 'main_function(varargin)' to a 'subfunction(varargin_new)'?
3 ビュー (過去 30 日間)
古いコメントを表示
The original function and subfuntion are large and misleading from the actual question so I will try to simplify it and provide as much information as possible.
function varargout = main_function(varargin)
% command: [a1,a2,a3] = ...
% ...main_function (num1,num2,num3, string_x, string-a1, string-a2, string-b1, string-b2,string-c1...)
% *1st entry of each 'pair' of strings(i.e. string-a1,b1)* will be used for some other purpose.
% it is necessary to use the 2nd entries string-a2,b2,... as an input to the subfunction.
% Note: first 3 input arguments and string_x are always same type of inputs,...
% ...only strings-a1,a2,b1,b2,c1.. changes size as the pairs of 2
% An example for: ...
% [a1,a2,a3] = mainfunction (num1,num2,num3, string_x, string-a1, string-a2, string-b1, string-b2)
A = varargin{1};
B = varargin{2};
C = varargin{3};
D = string(vararg{4});
%to create variables of input arguments(string-a2,b2,c2..) that depends on the user input
P = (nargin-4)/2; % calculate number of pairs given as input
for n= 1:P
jump1 = 2n+3; % to assign varargin{5} to the first variable that this loop will create
F = (varargin{jump});
G (1,n) = string(F); % to create a vector of 'F's
end
for n1=1:Pairs
Jump2 = n1+2 ; % as the 'subfuntion' already has varargin{1},{2}, so to start with...
% ...varargin{3} for string-a2 and so on...
% this will assign string-a2,b2.. to varargin{3},{4}...
eval(sprintf('varargin_n{%d-varL1+Lcall} = char(Lname(varL1))', varL1));
end
varargin_new = varargin; %to create new varargin_n for the subfuntion
% following will remove the initial varargin{} values with newone and also delete the empty cells.
% So new varargin will have the exact variables that are needed for the
% subfuntion(varargin_new) ~ ~ subfuntion(A,C, varargin{2}, varargin{4}, ...)
varargin_new{1} = A;
varargin_new{2} = C;
for remove = (2+P):max(size(varargin)) % to empty the cells of nargin_n that has...
varargin_new{remove} = {}; % ...unnecessary values of the original varargin.
end
varargin_new= varargin_new(~cellfun('isempty',varargin_new)); %to delete empty cells, so it will...
%...have correct size for the subfuntion.
subfuntion (varargin_new)
end
This does not work because it is taking varargin_new as 1 input variable and it shows that 2nd variable is not assigned to the subfunction.
When the code is executed with a breakpoint before the subfunction the generated varargin_new is in the following form:
command-window:
main_function(1,2,3,'a1','a2','b1','b2')
varargin_new :1×5 cell array
Columns 1 through 5
{[1]} {[2]} {[3]} {'a2'} {'b2'}
Could anyone please help me to understand, how to assign the 'varargin_new' to the subfunction?
If the information related to the question is insufficient, I apologise. Please ask anything.
0 件のコメント
採用された回答
Clayton Gotberg
2021 年 4 月 16 日
編集済み: Clayton Gotberg
2021 年 4 月 16 日
Solution
When you call the original function, you do so with
original_function(arg1,arg2,arg3,arg4,..)
Using varargin lets you pass any number of arguments by making a cell array
varargin = {{arg1},{arg2},{arg3},{arg4},..} % A 1xN cell array
The issue is that in the subfunction you are passing one argument -
varargin_new = {{new_arg1},{new_arg2},{new_arg3},{new_arg4},..} % A 1xM cell array
subfunction(varargin_new) % Giving only one argument, varargin_new
to varargin instead of passing separate arguments. This means that in the subfunction, your new varargin is
varargin = {{{new_arg1},{new_arg2},{new_arg3},{new_arg4},..}} % A 1x1 cell array
You can solve this by changing the expected input of the subfunction to the argument you are passing. Since you are using a single cell array instead of an unknown number of parameters, you don't need to use varargin at all.
Example
function output_A = function_A(varargin) % I don't know how many inputs will be passed to this
% function, so I use varargin to accomodate any number of inputs
new_varargin = changes_to(varargin); % Placeholder for whatever is in this function
output_A = function_B(new_varargin);
end
function output_B = function_B(input) %Note that I am not using varargin
% because I know there is exactly one input (even if I don't know what size
% that input is)
output_B = changes_to(input); % Placeholder for whatever is in the function
end
2 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!