views:

502

answers:

2

How do I call 3 MATLAB .m files in a loop and display the results in sequence?

+1  A: 

Assuming that your files are scripts containing the plotting commands, you can do the following:

mfiles = {'file1' 'file2' 'file3'};
for i=1:length(mfiles)
    eval( mfiles{i} );
    pause
end

where for example, we have:

file1.m

x = 0:0.1:2*pi;
plot(x, sin(x))
Amro
+4  A: 

Another option (in addition to Amro's) is to use function handles:

fileList = {@file1 @file2 @file3};  % A cell array of function handles
for iFile = 1:numel(fileList)
  fileList{iFile}();                % Evaluate the function handle
  pause                             % Wait for a keypress to continue
end

You can call a function using its handle as I did above or using the function FEVAL. If you have a function name in a string, you can use the function STR2FUNC to convert it to a function handle (assuming it isn't a nested function, which requires the function handle constructor @). The following example illustrates each of these alternatives:

fileList = {str2func('file1') str2func('file2') str2func('file3')};
for iFile = 1:numel(fileList)
  feval(fileList{iFile});           % Evaluate the function handle
  pause                             % Wait for a keypress to continue
end


How do the two answers differ?

You may be wondering what the difference is between my answer (using function handles) and Amro's (using strings). For very simple cases, you would likely see no difference. However, you can run into more complicated scoping and function precedence issues if you use strings for the function names and evaluate them with EVAL. Here's an example to illustrate...

Let's say we have two m-files:

fcnA.m

function fcnA
  disp('I am an m-file!');
end

fcnB.m

function fcnB(inFcn)
  switch class(inFcn)       % Check the data type of inFcn...
    case 'char'             % ...and do this if it is a string...
      eval(inFcn);
    case 'function_handle'  % ...or this if it is a function handle
      inFcn();
  end
end
function fcnA                   % A subfunction in fcnB.m
  disp('I am a subfunction!');
end

The function fcnB is designed to take either a function name or a function handle and evaluate it. By an unfortunate coincidence (or maybe intentionally) there is a subfunction in fcnB.m that is also called fcnA. What happens when we call fcnB in two different ways?

>> fcnB('fcnA')      % Pass a string with the function name
I am a subfunction!
>> fcnB(@fcnA)       % Pass a function handle
I am an m-file!

Notice that passing the function name as a string causes the subfunction fcnA to be evaluated. This is because at the time that EVAL is called the subfunction fcnA has the highest function precedence of all the functions named fcnA. In contrast, passing a function handle causes the m-file fcnA to be called instead. This is because the function handle is created first, then passed to fcnB as an argument. The m-file fcnA is the only one in scope (i.e. the only one that can be called) outside of fcnB, and is thus the one the function handle is tied to.

In general, I prefer to use function handles because I feel it gives me more control over which specific function is being called, thus avoiding unexpected behavior as in the above example.

gnovice
Nice Answer, Vote Up
Baget