简体   繁体   中英

How do I pass a string as a function argument in 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

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

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

How should I do this?

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 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.


Previous answer:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM