[Matlab] Function Handle-user input

In summary, the conversation discusses the possibility of using user inputs for function handles in Matlab. However, it is not possible as the user would need to input the entire function handle and not just a piece. Additionally, it is not possible to input a function handle that is both int32 and double, similar to asking for a matrix that is half int32 and half double.
  • #1
Bostonpancake0
42
0
Hi,

My question is in relation to Matlab.

I'm curious, is there anyway to employ user inputs for function handles?

By this I mean that the user is prompted to enter they're chosen function and the next line of code employs that into a handle.

My quick and rough attempt was:

a=input('enter a function in terms of x: ')
f=@(x) a

But I receive an error saying "Undefined function or variable x"

Any suggestions greatly appreciated.
 
Physics news on Phys.org
  • #2
I don't think that's possible. They would have to input the entire function handle, not just a piece.

Otherwise, what is a? If they input a number, or a matrix, then "@(x) a" just returns a, and isn't really a function. If they input a string like 'sin', then "@(x) a" just doesn't make any sense. They need to input a function handle, since it is a fundamental type.

It would be similar to asking, "Input a matrix that is half int32 and half double." Not possible. Matrices can be entirely int32 or entirely double, but not both.
 

1. What is a function handle in Matlab?

A function handle in Matlab is a variable that stores a reference to a function. It allows you to pass functions as arguments to other functions, or to use them as data in your code.

2. How do I create a function handle in Matlab?

You can create a function handle in Matlab by using the "@" symbol followed by the name of the function. For example, if you have a function named "myFunction", the handle would be created as "@myFunction".

3. How do I use a function handle to accept user input?

You can use a function handle to accept user input by passing it as an argument to the "input" function. For example, if you have a function handle named "myFunction", you can use it in the "input" function as follows:

input('Enter a number: ', @myFunction);

This will prompt the user to enter a number, and the input will be passed to the function referenced by the handle.

4. Can I pass multiple function handles as user input in Matlab?

Yes, you can pass multiple function handles as user input in Matlab by using cell arrays. Cell arrays allow you to store and access multiple function handles in a single variable. You can then use the cell array with the "input" function to accept user input for multiple functions.

5. How do I call a function using a function handle in Matlab?

You can call a function using a function handle in Matlab by using the "feval" function. This function takes the function handle as the first argument, and any additional arguments can be passed after that. For example, if you have a function handle named "myFunction", you can call it as follows:

feval(@myFunction, arg1, arg2);

This will call the function referenced by the handle and pass in the arguments "arg1" and "arg2".

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
820
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
19
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
Back
Top