I have four Matlab functions/scripts and I call all the functions/scripts from one main function, but the results where different when I run the individual functions/scripts. can any one help on how to resolve this issue

 채택된 답변

Star Strider
Star Strider 2017년 11월 19일

0 개 추천

Without seeing them, no.
If you are using global variables, remove the global calls and pass those variables as arguments to each of your functions instead.

댓글 수: 5

Bashir Yusuf Bichi
Bashir Yusuf Bichi 2017년 11월 19일
The global variables does not have significant role in the codes, but there are some variables that have the same identifiers.
Star Strider
Star Strider 2017년 11월 19일
The global variables should not have any role in your code! The problems you are seeing are exactly the reasons it is never a good idea to use them.
Functions have their own workspaces, so variables of the same names in different functions will not have any effect on each other, even if you name them as arguments. If you have them as global variables, you will get unpredictable results that are difficult — if not impossible — to debug, as you have discovered.
Bashir Yusuf Bichi
Bashir Yusuf Bichi 2017년 11월 19일
thank you so much, I removed the global variables and it works, I thought when sending a variable to both the functions I have to name such variable as global.
Stephen23
Stephen23 2017년 11월 19일
"I thought when sending a variable to both the functions I have to name such variable as global."
When passing variables as arguments (as you should be doing) you should not declare those variables as global.
Star Strider
Star Strider 2017년 11월 19일
@Bashir Yusuf Bichi — As always, my pleasure.
If my Answer helped you solve your problem, please Accept it!
—————
@Stephen — Good point.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

태그

질문:

2017년 11월 19일

댓글:

2017년 11월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by