declare global var by string name

3 visualizaciones (últimos 30 días)
Andy
Andy el 1 de Abr. de 2020
Comentada: Walter Roberson el 1 de Abr. de 2020
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 comentarios
Andy
Andy el 1 de Abr. de 2020
updated description ... any suggestions, welcomed.
Walter Roberson
Walter Roberson el 1 de Abr. de 2020
See https://www.mathworks.com/matlabcentral/answers/514509-global-variables-pattern-search#answer_423280 for some information about global variables in parallel processes.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 1 de Abr. de 2020
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 comentarios
Guillaume
Guillaume el 1 de Abr. de 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 el 1 de Abr. de 2020
some more updates above.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by