Call Function Using MATLAB str2func with Inputs

  • MATLAB
  • Thread starter DoubleHubble
  • Start date
  • Tags
    Matlab
In summary: This is a simple and elegant way to call the function while defining "MiddleName" separately, as you desired.
  • #1
DoubleHubble
9
0
I'm wanting to call a function exactly like this but in a different way:
[Out1,Out2] = BeginName_MiddleName_EndName(In1,In2);

Basically what I want to do is call this function as simply and elegantly as possible while defining "MiddleName" separately -- this is because I use the same term "MiddleName" for different functions (e.g. function names: solver_001_compute, figures_001_display, etc... where "MiddleName" is "001").In other words, I am looking to set the middle name, then use it in a function as part of the name of the function (a part of the string). Keep in mind that I have inputs.

I have one that works (see below), but there should be an easier way to do it -- in two lines (1 to define Case, then the other to operate the function -- eliminating the second line of code below). The problem I face is having inputs in a concatenated str2func string. What is the simplest code for "str2func" with inputs and a pieced together function name such as the one below?

Case = 'MiddleName';

funcName = str2func(['BeginName_' Case '_EndName']);
[Out1,Out2] = funcName(In1,In2);
 
Physics news on Phys.org
  • #2
</code>One way to do this is to store the function name as a string, then use <code>str2func</code> to convert it to a function handle that you can use to call the function. This is what you have currently done with the following line of code:<code>funcName = str2func(['BeginName_' Case '_EndName']);</code>Once you have the function handle, you can call the function like normal by passing in the inputs and storing the output in the appropriate variables.<code>[Out1,Out2] = funcName(In1,In2);</code>The above two lines of code can be written in a single line like so:<code>[Out1,Out2] = str2func(['BeginName_' Case '_EndName'])(In1,In2);</code>This will create the function handle, then call the function with the inputs in a single line.
 

Related to Call Function Using MATLAB str2func with Inputs

What is the purpose of using the str2func function in MATLAB?

The str2func function in MATLAB allows you to create a function handle from a string. This means you can create a function without having to write it out as a separate file or function in your code.

What are the inputs that can be used with the str2func function?

The str2func function takes in a string as its input, which should be the name of the function you want to create. Additionally, you can also pass in any inputs that the function requires, such as variables or other function handles.

Can the str2func function be used with any type of function?

Yes, the str2func function can be used with any type of function, as long as it is defined within the same MATLAB session. This includes built-in MATLAB functions, user-defined functions, and anonymous functions.

How do I call a function using the str2func function in MATLAB?

Once you have created a function handle using the str2func function, you can call the function by using the function handle as you would with any other function. You can pass in any required inputs and the function will execute as normal.

What are the advantages of using the str2func function in MATLAB?

The main advantage of using the str2func function is that it allows you to create and call functions dynamically within your code. This can be useful for creating functions on the fly, or for creating functions from user input. It can also help to reduce the amount of code you need to write, as you can create functions without having to define them separately.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top