String to actual function

1 回表示 (過去 30 日間)
Ninad Pimpalkhare
Ninad Pimpalkhare 2020 年 7 月 9 日
回答済み: madhan ravi 2020 年 7 月 9 日
Hi,
I have a string array A having 100 entries. The entries are like 1) X1+X2
2) X3+X4
3) X5+X10....etc
where X1, X2,....are variables already existing in the workspace and contain numerical data.
The string array A containing the functions as string, I need to execute it as in, if first entry of string is X1+X2, it pick the entry from the string and actually executes function X1+X2and stores the result in some other variable like H1.
can someone provide the code for it?
  2 件のコメント
Stephen23
Stephen23 2020 年 7 月 9 日
You would be much better off using arrays and function handles.

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

回答 (2 件)

Image Analyst
Image Analyst 2020 年 7 月 9 日
X1 = randi(9, 1, 5)
X2 = randi(9, 1, 5)
str = 'X1+X2'
H = eval(str)
X1 =
8 2 5 5 6
X2 =
7 7 3 7 6
str =
'X1+X2'
H =
15 9 8 12 12

madhan ravi
madhan ravi 2020 年 7 月 9 日
AVOID EVAL:
X1 = randi(9, 1, 5)
X2 = randi(9, 1, 5)
str = 'X1+X2'
H = subs(str2sym(str)) % requires symbolic toolbox

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by