How do i make this section of code a callable function, I keep getting errors

1 visualizzazione (ultimi 30 giorni)
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);

Risposte (1)

Walter Roberson
Walter Roberson il 2 Mag 2023
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 Commenti
Walter Roberson
Walter Roberson il 3 Mag 2023
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()

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by