[Matlab] Function Handle-user input

  • Context: MATLAB 
  • Thread starter Thread starter Bostonpancake0
  • Start date Start date
  • Tags Tags
    Function Input Matlab
Click For Summary
SUMMARY

The discussion centers on the limitations of using user inputs for function handles in Matlab. A user attempted to create a function handle using the input function, but encountered an error due to the undefined variable 'x'. It was concluded that users must input a complete function handle rather than a partial expression or a string representation of a function. The analogy drawn compares this situation to the impossibility of creating a matrix that is simultaneously of two different data types.

PREREQUISITES
  • Understanding of Matlab function handles
  • Familiarity with user input functions in Matlab
  • Knowledge of variable scope and definitions in Matlab
  • Basic concepts of data types in Matlab
NEXT STEPS
  • Explore Matlab documentation on function handles and their creation
  • Learn about the 'input' function and its limitations in Matlab
  • Investigate how to validate user inputs for function handles
  • Study examples of dynamic function creation in Matlab
USEFUL FOR

Matlab users, software developers, and educators looking to enhance their understanding of function handles and user input handling in Matlab.

Bostonpancake0
Messages
42
Reaction score
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
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K