Object handlers to modify objects across functions

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

All functions of what? What is wrong with passing it as an argument to those functions that need it? Is this a custom object that you have written? In which case, if you are familiar with object-oriented programming then 'access across all functions' would seem to imply that you want a class which encompasses all those functions and stores your matrix within it - then it is accessible to them.
classdef node < handle
properties
root
node_val
number_of_leaves
level
endproperties
methods
function obj = node(a,b)
obj.node_val = a;
obj.root = b;
endfunction
endmethods
endclassdef
The above is the class definition. This is the function for initialising the matrix.
function treemap()
for i = 1:3500
tree(i) = node(-1,-1);
endfor
endfunction
I call the treemap() from the main to initialise the tree and try to preform operations on the various nodes of the tree. I find that the initialisation is gone. The root and node_val of each node are no longer equal to -1.
Why do you keep putting things like endfunction and endfor? These are not valid syntax. From what you are saying though you are managing to at least run your code.
For me it works fine if I remove all those and replace them with just end
Obviously your tree is only in the scope of that function though so it won't exist outside of it at all, let alone your nodes within it no longer having the expected values.
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
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

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

Asked:

on 15 Oct 2018

Commented:

on 6 Nov 2018

Community Treasure Hunt

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

Start Hunting!