MATLAB Answers


How to rearrange the output variables for matlabFunction()?

Asked by William Sharpless on 17 Nov 2019 at 23:01
Latest activity Commented on by Star Strider
on 18 Nov 2019 at 1:18
I made a Jacobian with jacobian() and then turned it into a function with the matlabFunction(), but matlab rearranges the variables (in alpha numeric order). For example, it took a matrix of variables and put the tenth and eleventh row before the first row because a10_1,...a10_11 goes before a1_1 AND then within those variables it put the a10_10 and a10_11 after a11_1 ... a11_9. This is annoying because I now have to disect the matricies of data I will throw into the function and creates room for a subtle error. Is there a way to turn off the sort capability in matlabFunction()? docs lead me to sympref() and a couple name-value pairs of matlabFunction() but neither looked like they could rearrange the variables. This is in R2019a.
syms x [N 1]
syms u [N 1]
syms a [N N]
dX(:,1) = x.*(u+a*x);
J = matlabFunction(jacobian(dX(:,1),x))
J =
function_handle with value:
% ^^^^^ ^^^^^^^^^^^^ ^^^^^^ ^^^^^^


Sign in to comment.

2 Answers

Answer by Star Strider
on 17 Nov 2019 at 23:46
 Accepted Answer

I can’t run the code you posted, since it generates significant errors about matrix size multiplication.
However, a simple example demonstrates how to order the arguments:
syms a b c d x1 x2 x3 x4
e = a*x1+b*x2+c*x3+d*x4;
E = matlabFunction(e, 'Vars',{[x1 x2 x3 x4],c a b d})
E =
function_handle with value:
So the arguments are ordered in the sequence requested, and the parameters are grouped together as ‘in1’ in a separate parameter vector, in the event you need to do that.


Show 1 older comment
No worries.
My illustration holds. Experiment with this:
N = 11;
syms x [N 1]
syms u [N 1]
syms a [N N]
dX(:,1) = x.*(u+a*x);
av = reshape(a.', 1, []);
uv = reshape(u.', 1, []);
xv = reshape(x.', 1, []);
J = matlabFunction(jacobian(dX(:,1),x), 'Vars',{av,uv,xv})
The various vectors are ‘in1’ and such, however they should be in the order specified in ‘av’ and the others.
Yeah this is good stuff, thanks a bunch. The following line works well.
J = matlabFunction(jacobian(dX(:,1),x),'Vars', {x, a, u})
As always, my pleasure!

Sign in to comment.

Answer by the cyclist
on 17 Nov 2019 at 23:26

I don't know the answer to your sorting question.
However, pulling back to the big picture for a moment, it seems like storing all the variables separately (rather than in single variable, with indexing) is a bad idea. See, for example, this tutorial.
Back to the small picture: A tactical solution might be to name variables with 01 or 001 instead of just 1, to keep it alphabetically ahead of 10.

  1 Comment

mm ok, the latter seems like an alright work around, thx. Note from the code, these were the indeces matlab gave to them when I asked for a symbolic matrix, seems like matlab should do it properly or change the variable output arrangement.
I would like to know general answer still as I have run into this problem before.
In regards to the prior, how could I do this with symbolic math (it seems like what I did was what you said)? I don't intend to sort these independently, matlab just gives them to me that way after I perform the required operations.
In a perfect world, matlab might recognize that I have the full index of a matrix of input variables in my symbolic function and condense them.

Sign in to comment.