Search code examples
matlabsymbolic-math

passing a vector to a matlabfunction WITHOUT num2cell


For each of my simulation runs I generate a number of m-files using the matlabFunction command. these m-files are used to generate artificial potential fields. The number of (scalar) inputs may vary among different runs (due to the number of robots I am simulating), but stay constant during the run itself. Because each robot has its own potential field function I have to prepare input vectors for each of these robots. I am currently doing this in the following manner for each robot i:

Tvec = [T(1:i-1,i).' T(i,1:i-1) T(i,i+1:end) T(i+1:end,i).'];
Xvec = reshape(xN_1',1,(J+K)*2);
if sum(leaders==i)
    InputVector = num2cell([Tvec Xvec xd]);
else
    InputVector = num2cell([Tvec Xvec]);
end

fun=gradfun{i};
[gradx,grady] = fun(InputVector{:});

Short explanation of the above code: I have to prepare a transmission vector Tvec by extracting some values from matrix T. Furthermore I need the position vector Xvec, and pass these to the gradient function. If the robot is a 'leader', also the destination xd is added. gradfun is a cell of function handles that point to my generated potential field functions. InputVector is a cell array with scalar entries.

My problem here is that this approach is pretty slow due to the num2cell command, which takes up almost as much time as the actual gradient computation. Is there any way to bypass using the num2cell command? I am willing to edit the function inputs to the potential field functions, as long as this can be done from a matlab script (i.e. this should be possible to automate rather than manually change the comma-separated inputs to a vector-based input).


Solution

  • I have found a way to deal with the problem. For those who are interested: it is possible to manipulate the input variables for the function generated by matlabFunction with the vars option as follows:

    matlabFunction(symbolic_expression,'file',funname,'vars',{vector1,vector2,vector3});
    

    Where vector1,vector2,vector3 are symbolic vectors. In this way, the function file that is generated will accept vector inputs.