Reputation: 554
I have a uicontrol
of type 'edit' and I want to be able to write something like sin(2*10*pi*t)-sin(2*15*pi*t)
on it and then this string to become an expression for a variable F, for example: F=sin(2*10*pi*t)-sin(2*15*pi*t);
where t
is previously declared.
string=get(uicontrol_data, 'String');
%now I have the string value of the input
What should I do in order to transform the string into an expression?
Upvotes: 0
Views: 117
Reputation: 112769
You can avoid using eval
(which is generally not recommended) by means of str2func
:
str = get(uicontrol_data, 'String'); %// reads string grom uicontrol
f = str2func(['@(t)' str]); %// creates anonymous function and function handle
F = f(t); %// evaluates that function for the previously declared variable `t`
Upvotes: 1
Reputation: 5188
You have to use the built-in eval
function. For instance:
string = get(uicontrol_data, 'String');
eval(['F = ' string ';']); % Equivalent of F = ...;
Best,
Upvotes: 0