save the change to a variable in a function
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!