matlab parameters在哪,matlab變量中的函數名稱和參數

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值