save the change to a variable in a function

3 vues (au cours des 30 derniers jours)
越琪 吴
越琪 吴 le 8 Oct 2020
Commenté : Rik le 10 Oct 2020
In my project, I need to use some functions periodicly. But I found that even the value of a variable is changed in the first function, the second function still can't use the new value, how can I solve the problem if I don't want to use global?
Any help will be appreciated!
Thank you!
*part of my main code is shown below, t means time
if t>g && t<=g+o
plot(x1,y1,'.y','MarkerSize',35);c1=2;
plot(x2,y2,'.y','MarkerSize',35);c2=2;
plot(x3,y3,'.y','MarkerSize',35);c3=2;
plot(x4,y4,'.y','MarkerSize',35);c4=2;
cfnorth(w,north,z,cnt1,a1,b1);
cfeast(w,east,z,cnt2,a2,b2);
cfsouth(w,south,z,cnt3,a3,b3);
cfwest(w,west,z,cnt4,a4,b4);
movecars(z,jaywalk,w,p,cnt1,a1,b1,c1,f1,pass1,cnt2,a2,b2,c2,f2,...
pass2,cnt3,a3,b3,c3,f3,pass3,cnt4,a4,b4,c4,f4,pass4);
pause(1);
M(t)=getframe;
end
if t>g+o && t<=g+o+r
plot(x1,y1,'.g','MarkerSize',35);c1=3;
plot(x2,y2,'.r','MarkerSize',35);c2=1;
plot(x3,y3,'.g','MarkerSize',35);c3=3;
plot(x4,y4,'.r','MarkerSize',35);c4=1;
cfnorth(w,north,z,cnt1,a1,b1);
cfeast(w,east,z,cnt2,a2,b2);
cfsouth(w,south,z,cnt3,a3,b3);
cfwest(w,west,z,cnt4,a4,b4);
movecars(z,jaywalk,w,p,cnt1,a1,b1,c1,f1,pass1,cnt2,a2,b2,c2,f2,...
pass2,cnt3,a3,b3,c3,f3,pass3,cnt4,a4,b4,c4,f4,pass4);
pause(1);
M(t)=getframe;
end
...
  4 commentaires
Mohammad Sami
Mohammad Sami le 9 Oct 2020
If you use function, you need to return the variable that are changed. Then assign the outputs of the function. For example
if true
[A,B,C] = functionone(A,B,C,D,E);
[C,D,X] = functiontwo(A,B,C);
end
Rik
Rik le 10 Oct 2020
Comment posted as flag by 越琪 吴:
this answer exactly solve my problem! Thank you soooooooooooooooo much! My appreciation is beyond description!

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by