string 如何在 MATLAB 中将字符串作为函数参数传递?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1989919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:36:13  来源:igfitidea点击:

How do I pass a string as a function argument in MATLAB?

stringmatlabfunctionarguments

提问by ablimit

Basically, I have 10 data files and I wrote a MATLAB function to process these data. The code is like this:

基本上,我有 10 个数据文件,我编写了一个 MATLAB 函数来处理这些数据。代码是这样的:

function Z = fitdata(file_path)

  A = importdata(file_path,',');
  ...

end

Since I don't want to input the same command 10 times (for different file names), I wrote another script to automate this processing. The code looks like this:

由于我不想输入相同的命令 10 次(对于不同的文件名),我编写了另一个脚本来自动执行此处理。代码如下所示:

function X = automate()

  myarray = {'file_one', 'file_two', 'file_three',......,'file_ten'};
  for i = 1:9
    mypath = myarray{i};
    W = fitdata(mypath);
    ...
  end

end

But I'm getting the error "Too many input arguments" at the call to the fitdata(file_path) function.

但是我在调​​用 fitdata(file_path) 函数时收到错误“输入参数太多”。

How should I do this?

我该怎么做?

采纳答案by gnovice

EDIT:Since the suggestions below didn't solve the problem, and since there doesn't appear to be anything else wrong with the code you posted, I would next check to make sure the version of fitdatagiven above is the onlyfunction of that name on the MATLAB path. You may have inadvertently created another function or script and saved it as fitdata.m, and this may be getting called instead of the version you created above.

编辑:由于下面的建议没有解决问题,并且由于您发布的代码似乎没有任何其他问题,我接下来会检查以确保fitdata上面给出的版本是该名称的唯一功能在 MATLAB 路径上。您可能无意中创建了另一个函数或脚本并将其另存为fitdata.m,这可能会被调用而不是您在上面创建的版本。



Previous answer:

上一个答案:

I think you mean to use the IMPORTDATAfunction instead of IMPORT, which is the likely source of the error you are getting.

我认为您的意思是使用IMPORTDATA函数而不是IMPORT,这可能是您遇到的错误的来源。

One additional piece of advice: it's best not to name one of your variables path, since there is already a function PATH. The variable will end up being used instead of the function (based on the MATLAB precedence rules), which will still be what you want to happen in this specific case but is a source of confusion and error in other cases.

另一条建议:最好不要命名您的变量之一path,因为已经有一个函数PATH。最终将使用变量而不是函数(基于MATLAB 优先规则),这仍然是您希望在这种特定情况下发生的情况,但在其他情况下会导致混淆和错误。