I am writing a program for school. I need the user to input a function as a string, and then I need to process it.
What I am doing:
f = vectorize(inline(user_input_string)); x = linspace(0, 20, 20); y = f(x); ...
This works perfectly except when the user inputs a constant function (such as f = 1). In order for my project to work, y must be a vector. But if the user inputs a constant function, Matlab automatically sets y to a sclalar, instead of a vector.
What can I do?
No products are associated with this question.
I'm not certain I completely understand your problem. However, you could test for a scalar and if something like f=1 was the input, perhaps set y to:
f = '1'; y = polyval(str2num(f), x);
If the test for a scalar was true, you could also consider something like:
user_input_string = sprintf('polyval(%s, x)', f);
Without knowing more, that's the best solution I can come up with.