0

I'm writing a small MATLAB package and I'd like to ask for user input for a function. So if the user enters:

x.^2 + sin(x)

I want to use this user input to appear elsewhere in the code, but x would already be defined and so the expression above would be a vector (or scalar if length(x) is 1).

2 Answers 2

4

You can use the eval function for this. For example:

>> x = 5

x =

     5

>> eval('x*3')

ans =

    15
Sign up to request clarification or add additional context in comments.

Comments

2

You can create a function handle:

% some variable you already defined
myVar = 5;

% Create an anonymous function in some z 
f = str2func('@(z) z.^2 + sin(z)');

% Call function supplying the input
f(myVar)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.