Object handlers to modify objects across functions

조회 수: 3 (최근 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 CenterFile Exchange에서 Install Products에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by