How to plot a function, typed in a textfield (GUI), as a function handle?

Illustration
johncarter - 2020-08-27T11:39:38+00:00
Question: How to plot a function, typed in a textfield (GUI), as a function handle?

Why does the following don't work or what needs to be added?   % --- Executes on button press in pbCalculate. function pbCalculate_Callback(hObject, eventdata, handles) x=handles.VarXStart:handles.VarXRes:handles.VarXEnd; fh=@(x) handles.Function; plot(x,fh(x)) "handles.Function" contains for example "x.^2" or "sin(x)" (it's a string). Typed into a textfield in the GUI by a user. I've googled now a whole day long and couldn't find anything that worked.

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

Try this:

 

str='x.^2';
f=inline(str);
x=0:0.1:4;
plot(x,f(x))


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!