save the change to a variable in a function

조회 수: 1 (최근 30일)
越琪 吴
越琪 吴 2020년 10월 8일
댓글: Rik 2020년 10월 10일
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
Mohammad Sami
Mohammad Sami 2020년 10월 9일
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 2020년 10월 10일
Comment posted as flag by 越琪 吴:
this answer exactly solve my problem! Thank you soooooooooooooooo much! My appreciation is beyond description!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by