declare global var by string name

4 次查看(过去 30 天)
Andy
Andy 2020-4-1
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 个评论
Andy
Andy 2020-4-1
updated description ... any suggestions, welcomed.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2020-4-1
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 个评论
Guillaume
Guillaume 2020-4-1
"They want to be able to modify the contents of the global."
Then the variable should be returned as an output of DoSomework.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by