フィルターのクリア

How do I convert a string input to an executable equation?

49 ビュー (過去 30 日間)
Chris C
Chris C 2014 年 3 月 13 日
コメント済み: Stephen23 2017 年 10 月 17 日
I am developing a nice GUI for my company (those who don't want to code). It will give them areas to add their equations and define their constants, blah blah blah. The challenge I'm running into is grabbing the input string from the user and converting that to an executable equation or expression. Any thoughts?
  1 件のコメント
Stephen23
Stephen23 2017 年 10 月 17 日
inline is obsolete. Use str2func instead.

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

採用された回答

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014 年 3 月 13 日
you can simply use 'inline'
I've precviously written and equation solver, and it works like a charm, it also has a GUI and it solves any sort of euquation , if you enter them correctly, also assuming that the independet variable is always x
equ=get(handles.edit1,'Sring');
f=inline(equ,'x') % this makes function f with independent variable x, f(x)
Good Luck!
  2 件のコメント
Chris C
Chris C 2014 年 3 月 13 日
I'd like to be able to accept any system of linear or nonlinear equations which would potentially include a whole host of independent variables. Thanks for the help.
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014 年 3 月 13 日
You can define functions with more than one or two independet variables using inline.
Now that you got how to do it just take a look at MATLAB doc help to get master.
Glad that it helped ;)

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

その他の回答 (2 件)

Joseph Cheng
Joseph Cheng 2014 年 3 月 13 日
編集済み: Joseph Cheng 2014 年 3 月 13 日
Depending on what their equation uses eval (see http://www.mathworks.com/help/matlab/ref/eval.html) might work. if i remember correctly some toolboxes and functions are not supported if your GUI is compiled into a standalone. So put the edit box string into the eval
eval(get(handles.editbox1,'String'));
  2 件のコメント
Chris C
Chris C 2014 年 3 月 13 日
I don't want it to evaluate the code there though. I want it to hold it as an executable expression so that I can run the stacked executable equations through a differential equation solver. I believe eval has the code that I need, but I can't view the code. It's hidden.
sangeet pillai
sangeet pillai 2017 年 2 月 24 日
thank you joseph.your answer saved me a lot of time

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


Karan Gill
Karan Gill 2017 年 9 月 29 日
編集済み: Karan Gill 2017 年 10 月 17 日
For symbolic equations, starting R2017b, use str2sym.
>> syms f(x)
>> function1 = input('Please enter the function: ', 's');
Please enter the function: sin(x)
>> f(x) = str2sym(function1)
f(x) =
sin(x)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by