I've generated a matlab anonymous function. I get errors on calling it

2 views (last 30 days)
syms ax ay az bx by bz k double
a=[ax ay az]'
au=a./sqrt(ax^2+ay^2+az^2)
b=[bx by bz]'
bu=b./sqrt(bx^2+by^2+bz^2)
R=[bu(1)*au(1) bu(1)*au(2) bu(1)*au(3);
bu(2)*au(1) bu(2)*au(2) bu(2)*au(3);
bu(3)*au(1) bu(3)*au(2) bu(3)*au(3)]
c=R*a;
Crotated = c;
matlabFunction(Crotated,'file','testMatrix.m')
aa = [200. 100. 0.1];
bb = [200.13 99.995 0.12];
cc = [aa,bb]
Crotated(cc)
The error is:
Error using sym>checkindex (line 2429)
Index must be a positive integer or logical.
Error in sym>privformatmatrix (line 2387)
checkindex(x);
Error in sym>privformat (line 2362)
s = privformatmatrix(x);
Error in sym/subsref (line 1578)
[inds{k},refs{k}] = privformat(inds{k});
Error in rotations (line 26)
Crotated(cc)
Other times I got the error "Not enough input arguments". If I debug the program it seems that all the values are considered by Matlab to be the first variable in the function's list of variables. The other 5 are considered to be empty and I got the error. I've read some other similar questions, but didn't find a answer. Please explain, or name a simple solution to 'split' the values and give them to the other variables (from the function's list of input variables).

Accepted Answer

Star Strider
Star Strider on 11 Jun 2015
Your code produces this function file:
function Crotated = testMatrix(ax,ay,az,bx,by,bz)
%TESTMATRIX
% CROTATED = TESTMATRIX(AX,AY,AZ,BX,BY,BZ)
% This function was generated by the Symbolic Math Toolbox version 6.2.
% 11-Jun-2015 12:36:11
t2 = conj(ax);
t3 = conj(ay);
t4 = conj(bx);
t5 = ax.^2;
t6 = ay.^2;
t7 = az.^2;
t8 = t5+t6+t7;
t9 = 1.0./sqrt(t8);
t10 = bx.^2;
t11 = by.^2;
t12 = bz.^2;
t13 = t10+t11+t12;
t14 = 1.0./sqrt(t13);
t15 = conj(az);
t16 = t2.^2;
t17 = t3.^2;
t18 = conj(by);
t19 = t15.^2;
t20 = conj(bz);
Crotated = [t4.*t9.*t14.*t16+t4.*t9.*t14.*t17+t4.*t9.*t14.*t19;t9.*t14.*t16.*t18+t9.*t14.*t17.*t18+t9.*t14.*t18.*t19;t9.*t14.*t16.*t20+t9.*t14.*t17.*t20+t9.*t14.*t19.*t20];
Your function is called testMatrix, not Crotated, so you need to call it as:
Crotated = testMatrix(aa(1),aa(2),aa(3),bb(1),bb(2),bb(3));
if that is what you intend.
  2 Comments
Horia Popa
Horia Popa on 12 Jun 2015
That was what I intended, but still, isn't it possible not to specify the parameters explicitly, like you said: aa(1),aa(2),aa(3),bb(1),bb(2),bb(3) ? And just pass to the function a vector and the function split the fields between the 6 arguments. In plain English to say something like: Crotated = testMatrix(aa,bb); and the function to return a value (vector of 3 in this case)?
Star Strider
Star Strider on 12 Jun 2015
It is, but you have to edit the function a bit.
Change the first line to:
function Crotated = testMatrix(aa,bb)
then just after the comments (and just before the ‘t2’ assignment), insert this line:
[ax,ay,az,bx,by,bz] = deal(aa(1),aa(2),aa(3),bb(1),bb(2),bb(3));
The deal function is an efficient way of doing the multiple assignments in one line. The rest of your code doesn’t change.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!