eykanal
eykanal

Reputation: 27027

routine to either evaluate code or transmit via udp

I have a set of code that, depending on how the program is initiated, will either be executed locally or sent to a remote machine for execution. The ideal way I imagine this could work would look something like the following:

line_of_code = 'do_something_or_other();';

if execute_remotely
    send_via_udp(line_of_code);
else
    eval(line_of_code);
end

The thing is, I know that the eval() function is ridiculously inefficient. On the other hand, if I write out line_of_code in each section of the if block, that opens the door for errors. Is there any other way that I can do this more efficiently than by simply using eval()?

Upvotes: 1

Views: 169

Answers (1)

gnovice
gnovice

Reputation: 125864

EDIT: After more consideration and some discussion in the comments, I have my doubts that function handles can be transmitted via UDP. I'm therefore updating my answer, instead suggesting the use of the function FUNC2STR to convert the function handle to a string for transmission, then using the function STR2FUNC to convert it back to a function handle again after transmission...

To get around using EVAL, you can use a function handle instead of storing the line of code to be executed in a string:

fcnToEvaluate = @do_something_or_other;  %# Get a handle to the function

if execute_remotely
  fcnString = func2str(fcnToEvaluate);   %# Construct a function name string
                                         %#   from the function handle
  send_via_udp(fcnString);               %# Pass the function name string
else
  fcnToEvaluate();                       %# Evaluate the function
end

The above assumes that the function do_something_or_other already exists. You can then do something like the following on the remote system:

fcnString = receive_via_udp();        %# Get the function name string
fcnToEvaluate = str2func(fcnString);  %# Construct a function handle from
                                      %#   the function name string
fcnToEvaluate();                      %# Evaluate the function

As long as the code (i.e. m-file) for the function do_something_or_other exists on both the local and remote systems, I think this should work. Note that you could also use FEVAL to evaluate the function name string instead of converting it to a function handle first.

If you need to create a function on the fly, you can initialize fcnToEvaluate as an anonymous function in your code:

fcnToEvaluate = @() disp('Hello World!');  %# Create an anonymous function

And the code to send, receive, and evaluate this should be the same as above.

If you have arguments to pass to your function as well, you can place the function handle and input arguments into a cell array. For example:

fcnToEvaluate = @(x,y) x+y;  %# An anonymous function to add 2 values
inArg1 = 2;                  %# First input argument
inArg2 = 5;                  %# Second input argument
cellArray = {fcnToEvaluate inArg1 inArg2};  %# Create a cell array

if execute_remotely
  cellArray{1} = func2str(cellArray{1});  %# Construct a function name string
                                          %#   from the function handle
  send_via_udp(cellArray);                %# Pass the cell array
else
  cellArray{1}(cellArray{2:end});  %# Evaluate the function with the inputs
end

In this case, the code for send_via_udp may have to break the cell array up and send each cell separately. When received, the function name string will again have to be converted back to a function handle using STR2FUNC.

Upvotes: 3

Related Questions