How do i make this section of code a callable function, I keep getting errors
조회 수: 2 (최근 30일)
이전 댓글 표시
function [dx,dy,y,x,state]=position(N,i)
for i=1:N
if state(i)==1 % checks if ligand is active and give new position
dx=randi([-360,360],1);
dy=randi([-50,50],1);
x(i)=x(i)+dx;
y(i)=y(i)+dy;
end
end
x=mod(x,L);
y=mod(y,L);
댓글 수: 0
답변 (1개)
Walter Roberson
2023년 5월 2일
What is the point of passing in i as the second parameter, if in the very first line you are going to overwrite i because of the for i loop ?
if state(i)==1 % checks if ligand is active and give new position
You never assign to state and state is not passed in. We can tell from the fact there is no end matching the function that this is not a context in which you might potentially be sharing a variable. So for the state(i) part to work, state would have to be a function that accepts at least one input. But if it is a function, then why are you trying to output state as the 5th output ?
댓글 수: 3
Walter Roberson
2023년 5월 3일
Maybe you should be passing state into the function instead of passing the useless i into the function.
However, if you are never assigning to state inside the function, it is not obvious why you would want to return it as output -- unless you were operating inside a context that expects to pass state and expects that you might have changed the state, such as can happen if you are using a outputfcn or plotfcn callback for one of the optimizers such as ga()
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!