String containing variables to numerical array

I have a user inputed funciton, that is classed as characters. I want to replace those characters with their now assigned variables to return a numerical array.
Here is the code:
Inputs = inputdlg({['Please input the function you wish to plot in terms of X and Y' newline 'Function ='],'x-interval =','y-interval='});
InputFunction = Inputs{1,1};
x_interval = Inputs{2,1};
y_interval = Inputs{3,1};
x_interval = str2num(x_interval);
y_interval = str2num(y_interval);
[X,Y] = meshgrid(x_interval,y_interval);
InputFunction = str2double(InputFunction)
surf(X,Y,InputFunction)
e.g. function should be given in the form X.*exp(-X.^2-Y.^2), and then evaluated over the extracted intervals.
However (unsurpisingly) str2double(InputFunction) doesn't work.
Any help or suggestions much appreciated.

 採用された回答

Matt J
Matt J 2022 年 12 月 19 日
編集済み: Matt J 2022 年 12 月 19 日

0 投票

f=str2func("@(X,Y) " + InputFunction);
surf(X,Y,f(X,Y))

3 件のコメント

Paul
Paul 2022 年 12 月 19 日
Shouldn't this be str2func ?
Matt J
Matt J 2022 年 12 月 19 日
Yes!
Isaac
Isaac 2022 年 12 月 20 日
Thanks for the help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

リリース

R2022b

質問済み:

2022 年 12 月 19 日

コメント済み:

2022 年 12 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by