Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 819941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:23:34+00:00 2026-05-15T02:23:34+00:00

I have a set of code that, depending on how the program is initiated,

  • 0

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()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:23:35+00:00Added an answer on May 15, 2026 at 2:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.