How to insert a function into a GUI

Hi.
I'm making a simple GUI which receives functions and do operations on them. It is complete, however to change the function I need to modify the code and change the function.
Is there anyway to insert a funcion into a GUI as a string and converting it into a function?
e.g: the user inserts x.^2+x
The code receives it as string and converts it to a variable so that
y = x.^2 + x (the x will be defined beforehand)
Thanks in advance. I hope I've expressed my doubt clearly enough. Daniel.

답변 (2개)

Image Analyst
Image Analyst 2012년 12월 9일

1 개 추천

Yes, just go ahead and enter it in any place that is not another function. For example at the very end of your code, add this
function y = MyPolynomial(polynomialString, x)
coefficients = ParseString(polynomialString)
y = coefficients(1) * x^2 + coefficients(2) * x + coefficients(3);
function coefficients = ParseString(polynomialString)
equalsLocations = findstr(polynomialString, '=');
plusLocations = findstr(polynomialString, '+');
minusLocations = findstr(polynomialString, '-');
% etc. You do the rest.

댓글 수: 1

Daniel Jaló
Daniel Jaló 2012년 12월 9일
Thanks for the answer, but that won't allow me to insert any function at will, only a second degree polinomial function. I gave the function as an example, my objective is to be able to convert any function I insert.
Thanks. Daniel

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2012년 12월 10일

1 개 추천

y = str2func(['@(x) ', TheString]);

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2012년 12월 9일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by