Object handlers to modify objects across functions

2 views (last 30 days)
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 Comments
Diptangshu Sen
Diptangshu Sen on 5 Nov 2018
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 on 6 Nov 2018
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.

Sign in to comment.

Accepted Answer

TADA
TADA on 5 Nov 2018
Edited: TADA on 6 Nov 2018
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 Comment
TADA
TADA on 6 Nov 2018
Edited: TADA on 6 Nov 2018
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;

Sign in to comment.

More Answers (0)

Categories

Find more on Install Products in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!