Hello! I´ve created a global variable in a main GUI. A buttom on this opens a second GUI where I need to use the global variable. Must I declare:
global MyVariable;
in every function where I will use MyVariable inside the second GUI? Or there is a way to declare it once?

댓글 수: 1

Stephen23
Stephen23 2017년 10월 30일
@Brian Damian Luna: you would be best off avoiding global variables entirely. Passing arguments is much more reliable, and much easier to debug.

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

 채택된 답변

Jan
Jan 2017년 10월 29일
편집: Jan 2017년 10월 29일

0 개 추천

You have to declare a variable as global in each function you want to use it, of course. Otherwise it would be easy to let Matlab crash massively by declaring any frequently used variables like "x" or "K" globally as global.
It is explained in the documentation clearly, how globals are used. Therefore a
doc global
would help already.
Note that global variables are known to cause troubles and impede the debugging. Clean code does not contain any global variables for this reason. Better store the data in a figure and share its data using e.g. the ApplicationData.

댓글 수: 2

Brian Damian Luna
Brian Damian Luna 2017년 10월 29일
Thanks Jan! I'm beginner in the programming world.
Regards, Brian
Iwan Schulga
Iwan Schulga 2020년 5월 26일
how is it working?

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

추가 답변 (0개)

카테고리

제품

질문:

2017년 10월 29일

댓글:

2020년 5월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by