save the change to a variable in a function

5 ビュー (過去 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 Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by