开发者

In MATLAB, How I can run multiple files .m (M-file) automatically?

开发者 https://www.devze.com 2022-12-21 07:31 出处:网络
In MATLAB, how I can run 20 files开发者_开发技巧 .m (M-file) automatically?Make another M-file and put all of the names of your 20 existing M-files in that.

In MATLAB, how I can run 20 files开发者_开发技巧 .m (M-file) automatically?


Make another M-file and put all of the names of your 20 existing M-files in that.

If you want them to run on startup, put them in startup.m in the startup directory (see doc startup).

If they have systematic names, you can put the following in a loop:

[y1, y2, ...] = feval(function, x1, ..., xn)

where function is a string that you develop in the loop.

Edit: if the M-files are scripts rather than functions it is safer for future versions to use:

eval(s)

where s is the name of the script.


There are lots of ways, depending on what behaviour you want. MATLAB is a very flexible environment for this kind of stuff. If your files are in c:\work\myTwentyFiles, create a new file "runMyFiles.m" containing

function runMyFiles()
myDir = 'c:\work\myTwentyFiles';

d = dir([myDir filesep '*.m']);
for jj=1:numel(d)
    try
        toRun = fullfile(myDir, d(jj).name);
        fprintf('Running "%s"', toRun);
        run(toRun)
    catch E
        % Up to you!
    end
end

and then use the "-r" option to make MATLAB run this file automatically:

matlab -r runMyFiles

There are many other variations -- the hard-coded location of the MATLAB files looks unattractive for starters...

Just spotted the updated question: another option is to use a cell-array of functions to call

d = {'myfun1','myfun2', 'myfun3'};

and do something similar to the example above -- you'll need to change the definition of "toRun" to something like

toRun = fullfile(myDir, d{jj});
0

精彩评论

暂无评论...
验证码 换一张
取 消