如何在 MATLAB 中将字符串作为函数参数传递? [英] How do I pass a string as a function argument in MATLAB?

查看:202
本文介绍了如何在 MATLAB 中将字符串作为函数参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

function Z = fitdata(file_path)

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

end

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

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:

function X = automate()

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

end

但我在调用 fitdata(file_path) 函数时收到错误输入参数过多".

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

我该怎么做?

推荐答案

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

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 fitdata given above is the only function 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.

上一个答案:

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

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

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

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.

这篇关于如何在 MATLAB 中将字符串作为函数参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆