Object handlers to modify objects across functions

2 ビュー (過去 30 日間)
Diptangshu Sen
Diptangshu Sen 2018 年 10 月 15 日
コメント済み: Adam 2018 年 11 月 6 日
I want to create a matrix of objects(eg: nodes). The matrix needs to be accesses across all functions. Initializing the matrix as 'global' does not seem to work. Could anybody point me to how I can go about doing this using object handlers?
  6 件のコメント
Diptangshu Sen
Diptangshu Sen 2018 年 11 月 5 日
Please ignore the syntax. All of these are simply end and nothing more. The problem is still the same. I have a class called node that I have created. I have a function called treemap() where I use these nodes to prepare a tree. Now, this tree has to be accesses across a lot of other functions so that the different nodes can be updated as and when required. I tried making the tree global, but I get an error saying: no conversion for assignment of object to indexed matrix. Sorry for the late reply though. Thanks in advance. Sen
Adam
Adam 2018 年 11 月 6 日
As per my first comment, either passing it as an argument to those functions that need it or creating a class that connects the tree object to the functions that act on it seem to be the obvious solutions.

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

採用された回答

TADA
TADA 2018 年 11 月 5 日
編集済み: TADA 2018 年 11 月 6 日
to use a global var you should declare that tree is a global var inside your function treemap as well as in your main something like that:
function treemap()
global tree;
for i = 1:3500
tree(i) = node(-1,-1);
end
end
I personally hate global variables, they tend to cause more problems than they solve.
You can simply generate the instance of your tree in the tree function and return it as an output argument to your main, which is probably better practice than editing global variables.
If the problem is you don't want to pass it as an argument around to all your routines, then you can use a persistent variable in your tree function instead of a global
function tree = treemap()
persistent root;
if isempty(root)
root = node.empty();
% The backwards vector is for allocating the entire vector once
for i = 3500:-1:1
root(i) = node(-1,-1);
end
end
tree = root;
end
Now every time you invoke treemap you will get the same instance, this keeps the "dirty" solution in one place while a global is declared in every function it is used, so its easier to maintain like that
if you're using an OOP approach, you might as well implement some OOP design patterns for your problem. So you can have a treemap class which exposes this instance as a singleton Check out Singleton design pattern as an alternative
In Matlab you implement singleton using persistent variables like the example above.
or simply as a static persistent variable, which is essentially identical to the persistent function
  1 件のコメント
TADA
TADA 2018 年 11 月 6 日
編集済み: TADA 2018 年 11 月 6 日
by the way, if you want to clear your persistent instance, it is a bit more tedious than with global variables. but all you need to do is clear the function, in this case
clear treemap;
should clear the persistent variable.
if this is a nested function, you can clear the file that contains it
lets say treemap is a nested function inside your main you can clear it like that
clear main;

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by