declare global var by string name

within a function:
function DoSomeWork(globalVarName, var2, var3, etc)
global globalvarName ???
// so something with globalVarName such that changes on it get reflected outside function.
// however, globalVarName is specified as a string passed to function
end
main matlab file:
global gVar1 gVar2
DoSomeWork('gVar1',...)
DoSomeWork('gVar2',...)
Is there a way to achieve this?
Some more background:
Somewhere else, in a .NET application, a ZeroMQ publisher is pusing messages into two TCP ports:
publisherA = new ZSocket(ZSocketType.PUB);
publisherA.Bind("tcp://127.0.0.1:" + TcpPortA); // tcp = 5550
publisherB = new ZSocket(ZSocketType.PUB);
publisherB.Bind("tcp://127.0.0.1:" + TcpPortB); // tcp = 5501
string data = "abc"
if(publisherA != null) { publisherA.Send(new ZFrame(data)); }
if(publisherB != null) { publisherB.Send(new ZFrame(data)); }
Hundreds of messages per second flood the two TCP publishers ...
Matlab on the other hand, is supposed to listen and process these messages.
In Matlab, one needs to include JeroMQ:
javaaddpath('C:\Users\<username>\Documents\JeroMQ\jeromq-0.5.1.jar','-end)
import org.zeromq.*
global objectX
queA = parallel.pool.DataQueue();
lisA = afterEach(queA, @getAToken);
queB = parallel.pool.DataQueue();
lisB = afterEach(queB, @getBToken);
fObjA = parfeval(@GetAMessageLoop, 0, queA, 'Publisher A Name', '5550');
fObjB = parfeval(@GetBMessageLoop, 0, queB, 'Publisher B Name', '5551');
% objectX gets modified within the Message Loop Functions above ...
% other 'read only' processing may happen on objectX in the global space ... isolated from the updating threads above
Now, one can do some processing based on objectX contents above ... in the main thread.
Once done, one can close the queues:
cancel(fObjA);
cancel(fObjB);
delete(fObjA);
delete(fObjB);
delete(lisA);
delete(lisB);
Objective is - some global objects will be updated by the messages received over JeroMQ and be read by other functions...
One way I thought to 'parallelize this' is ... to pass the global object as a string name into a function. This object then becomes the recipient of one message queue. And another object becomes the recipient of another message queue. This allows having multiple global objects persistent in the global space, updated by JeroMQ listening threads ... in parallel.

4 commentaires

John D'Errico
John D'Errico le 1 Avr 2020
Ugh. Why would you want to do that? It is virtually pleading to create nasty bugs.
Stephen23
Stephen23 le 1 Avr 2020
Ugh ugh ugh... Combine together the two worst approaches to writing MATLAB code:
to force yourself into writing slow, inefficient, obfuscated, buggy, hard-to-debug code.
It would be much better to pass the data using the recommended and efficient methods of input/output arguments and/or nested functions:
Andy
Andy le 1 Avr 2020
updated description ... any suggestions, welcomed.
Walter Roberson
Walter Roberson le 1 Avr 2020
See https://www.mathworks.com/matlabcentral/answers/514509-global-variables-pattern-search#answer_423280 for some information about global variables in parallel processes.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 1 Avr 2020

0 votes

Yes there is. However, you should not do this.
For example what happens if the user passes in a name that is the same as one of the parameters to the function, then inside the function when you go to read or write from the user-supplied variable name, do you get the global variable or do you get the parameter?

4 commentaires

Guillaume
Guillaume le 1 Avr 2020
Modifié(e) : Guillaume le 1 Avr 2020
I would really emphasise the you should not do this.
You're combining the two worst design patterns in matlab: global variables and dynamic variable names. One is bad enough but two! Something has really gone wrong with your design.
There is a simple way to modify your design to get rid of both at once. Instead of passing the name of the variable and then accessing its content through global, simply pass the content of the variable.
function DoSomeWork(something, var2, var3, etc)
%no need for global variable anymore
%simply use something in your code as is
%...
end
%main script
Var1 = ..
Var2 = ..
DoSomework(Var1, ..)
DoSomework(Var2, ..)
Walter Roberson
Walter Roberson le 1 Avr 2020
Modifié(e) : Walter Roberson le 1 Avr 2020
They want to be able to modify the contents of the global.
Handle objects might possibly make sense.
Guillaume
Guillaume le 1 Avr 2020
"They want to be able to modify the contents of the global."
Then the variable should be returned as an output of DoSomework.
Andy
Andy le 1 Avr 2020
some more updates above.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Variables dans Centre d'aide et File Exchange

Question posée :

le 1 Avr 2020

Community Treasure Hunt

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

Start Hunting!

Translated by