error when calling a function inside of another function

I'm very new to matlab function. so sorry if i asked a stupid question. i got an error saying "The expression to the left of the equals sign is not a valid target for an assignment." for the line as:
answer = function2(x,y,z);
in function1. when I run another .m file which call this function1 and this function1 calls function2 like the above. I've tested the function2 at the command line with no error. which returns a (1,3) vector. and in function1, i didn't define the variable "answer". I tried to modify the function2 to make it return 3 output variable with each being a scalar and even reduce the function2 output to only one scalar output, it still gave the same error. could anybody give me some help? I really appreciate that.

1 个评论

Please show us the exact code line. There might be something subtle about what you used.

请先登录,再进行评论。

 采纳的回答

In orbit, line 16, you open a { and close } before the end, last line. Remove them.
Then variables r0 and a are not defined anywhere in the function.
Oleg

更多回答(2 个)

ok, here is the function1 and the e_field() is the function2,thanks
function [pos_arr]=orbit(qom_in, pos, vel, dt)
x(1:3) = pos(1);
y(1:3) = pos(2);
z(1:3) = pos(3);
vx(1:3) = vel(1);
vy(1:3) = vel(2);
vz(1:3) = vel(3);
pos_arr = [];
ii = 1;
rad = sqrt(x(1)^2 + y(1)^2);
rho = sqrt((rad - r0)^2 + z(1)^2);
while((rad<(r0+a))&&(rad>(r0-a))&&(rho<a)){
e = e_field(x(1), y(1), z(1));
b = b_field(x(1), y(1), z(1));
ax(2:4) = ax(1:3);
ay(2:4) = ay(1:3);
az(2:4) = az(1:3);
vx(2:4) = vx(1:3);
vy(2:4) = vy(1:3);
vz(2:4) = vz(1:3);
x(2:4) = x(1:3);
y(2:4) = y(1:3);
z(2:4) = z(1:3);
ax(1) = qom_in*(e(1) + vy(1)*b(3) - vz(1)*b(2));
ay(1) = qom_in*(e(2) + vz(1)*b(1) - vx(1)*b(3));
az(1) = qom_in*(e(3) + vx(1)*b(2) - vy(1)*b(1));
vx(1) = vx(2) + (ax(1) + ax(2))*dt/2;
vy(1) = vy(2) + (ay(1) + ay(2))*dt/2;
vz(1) = vz(2) + (az(1) + az(2))*dt/2;
x(1) = x(2) + (vx(1) + vx(2))*dt/2;
y(1) = y(2) + (vy(1) + vy(2))*dt/2;
z(1) = z(2) + (vz(1) + vz(2))*dt/2;
ii =ii+1;
rad = sqrt(x(1)^2 + y(1)^2);
rho = sqrt((rad - r0)^2 + z(1)^2);
pos_arr = [pos_arr; [x(1), y(1), z(1)]];
}end

类别

帮助中心File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by