global variables not cleared after 'clear;clc'

I have a global variable. I run the code for several times. I put 'clear;clc' at the front. Strangely before the global variable is called, it already has a value. However I rerun it, it always has a value before its line is called. What could've happened?

 採用された回答

Walter Roberson
Walter Roberson 2018 年 12 月 10 日

0 投票

clear by itself only removes variables from the current workspace not the global workspace . You need to
clear global

5 件のコメント

madhan ravi
madhan ravi 2018 年 12 月 10 日
Exactly as sir Walter says, that’s why most of the matlab experts do not recommend global variables.
Walter Roberson
Walter Roberson 2018 年 12 月 10 日
This aspect is relatively far down on my reasons for not recommending global variables. ;-)
madhan ravi
madhan ravi 2018 年 12 月 10 日
I agree sir Walter , what I mean't was using global variables makes debugging difficult causing a lot of confusions.
a a
a a 2018 年 12 月 10 日
I love using global variables to save writing functions with a long list of argin's. Is it a legitimate reason?
Walter Roberson
Walter Roberson 2018 年 12 月 10 日
using global variables to save writing functions with a long list of argin's. Is it a legitimate reason?
There is a big difference between "legitimate" reasons and "good" reasons. Making a bad programming decision because your mind is muddled by your cold and flu medication is a "legitimate" reason. Delegating the decision to bald hampsters for cost-savings reasons is a "legitimate" reason. Using global variables because your astrologer slash homoepathic guru is a "legitimate" reason. But these are not good reasons.
If you want to save writing long lists of arguments, then there are multiple strategies, including using nested functions with shared variables.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Preprocessing についてさらに検索

質問済み:

a a
2018 年 12 月 10 日

コメント済み:

2018 年 12 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by