How to declear global array and how to use it ?

44 ビュー (過去 30 日間)
Isee You
Isee You 2013 年 5 月 4 日
I want to declare global array and i want to use it in different function.
how to add data to this array without deleting the other data that are already on it

採用された回答

Jan
Jan 2013 年 5 月 4 日
編集済み: Jan 2013 年 5 月 4 日
It is a good programming style to avoid globals. They have the bad disadvantage, that it is very hard to find out, which part of the program is responsible for the last modification in case of problems. Therefore they impede debugging.
If you think, you have a really good reason to use globals, you have to declare them in each function. Use a very specific name, to avoid conflicts with other programs:
function out = myFunc1(in)
global myToolbox_Global
myToolbox_Global = [myToolbox_Global, now];
out = in; % A silly example code...
end
function out = myFunc2(in)
global myToolbox_Global
myToolbox_Global = [myToolbox_Global, now];
out = in; % A silly example code...
end
Well, "myToolbox_Global" is not very specific. An abbreviation of your name and the name of the program would be better. Remember that any globals are an invitation for bugs caused by unintended overwriting of values.
  3 件のコメント
Isee You
Isee You 2013 年 5 月 5 日
so what is the way i can use to help me to save data throw functions in one variable so i can use it after that
Jan
Jan 2013 年 5 月 5 日
This was an arbitrary example to show, how new value is appended to a global variable whenever the function is called:
myToolbox_Global = [myToolbox_Global, now];
Because it is an example only, it does not really mean anything.
A good programming practice is to deliver a variable, which is used in different functions, as input and output.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 4 日
編集済み: Azzi Abdelmalek 2013 年 5 月 4 日
Example
global a
a=[1 2 3]
a=[a 4 5]
Look at concatenation

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by