declare global var by string name

조회 수: 8 (최근 30일)
Andy
Andy 2020년 4월 1일
댓글: Walter Roberson 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.
Walter Roberson
Walter Roberson 2020년 4월 1일
See https://www.mathworks.com/matlabcentral/answers/514509-global-variables-pattern-search#answer_423280 for some information about global variables in parallel processes.

댓글을 달려면 로그인하십시오.

답변 (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.
Andy
Andy 2020년 4월 1일
some more updates above.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by