How to supply perticular varargin entries of the 'main_func​tion(varar​gin)' to a 'subfuncti​on(varargi​n_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.

采纳的回答

Clayton Gotberg
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 个评论
Garvit Amipara
Garvit Amipara 2021-4-16
编辑:Garvit Amipara 2021-4-16
"You can solve this by changing the expected input of the subfunction to the argument you are passing."
@Clayton Gotberg you are right! I changed the expectation of the subfunction input arguments by using struct2cell to get cell values and str2double for numerical variables. It worked perfectly.
Thank you so much for the quick solution to a big problem!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by