save the change to a variable in a function
1 view (last 30 days)
Show older comments
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 Comments
Mohammad Sami
on 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
Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!