In my matlab m-file I am using some logic (string concat) to build variables like this:
在我的matlab m文件中,我使用一些邏輯(字符串concat)來構建這樣的變量:
c = 'CalcPrediction(1,10)'
That means I have a string that is a function and some parameters. How can I do that function call?
這意味着我有一個字符串,它是一個函數和一些參數。我該怎么做那個函數調用?
Trying run(c) results in:
嘗試運行(c)會導致:
>> run(c)
??? Error using ==> run at 71
CalcPrediction(1,10) not found.
Note: run(c) works fine if there is no parameters. E.g. c='CalcPrediction'; run(c);
注意:如果沒有參數,run(c)可以正常工作。例如。 C = 'CalcPrediction';運行(C);
3 个解决方案
#1
The command you are looking for is eval() instead of run()
您正在尋找的命令是eval()而不是run()
#2
Without actually seeing the script it's hard to generalize, but...
沒有實際看到腳本,很難概括,但......
Where squareRoot is an m-file containing only :y=sqrt(x)
其中squareRoot是僅包含的m文件:y = sqrt(x)
Then executing :
然后執行:
x=[2,0];
c='squareRoot';
run(c);
gives :
y =
1.4142 0
This example is to say you can define the script to use a declared variable (x in this case) and then declare the variable before running the script.
這個例子就是說您可以定義腳本以使用聲明的變量(在本例中為x),然后在運行腳本之前聲明變量。
Without the script I don't know what you're doing with the parameters. If this doesn't answer your question, post your script.
沒有腳本,我不知道你在做什么參數。如果這不能回答您的問題,請發布您的腳本。
#3
You want to use str2func. This function takes a string and returns a function handler that can be called with your parameters. Check out the examples on the linked page.
你想使用str2func。此函數接受一個字符串並返回一個可以使用您的參數調用的函數處理程序。查看鏈接頁面上的示例。
fh = str2func('CalcPrediction')
fh(1, 10)