feelfree
feelfree

Reputation: 11753

How to call command program in MATLAB

In MATLAB it is possible to call command programs written in C or C++. For example, I can use the following command to call a command program in Windows:

system('program.exe -i input_file1.txt -o output_file1.txt -m 1 ');

By doing so, I can invoke command line program directly from MATLAB. The problem I have now is that all the arguments must be fixed when I use the system function. If one argument, for example, is changeable, then using system function will fail. For instance,

for i=1:3
  input_file_name = [num2str(i),'.txt'];
  system('program.exe -i input_file_name -o output_file1.txt -m 1 ');
end

Then, how can I deal with this situation? Thanks.

Upvotes: 0

Views: 218

Answers (2)

nkjt
nkjt

Reputation: 7817

More generally you can use sprintf to construct strings to pass to system, for example something like:

for n=1:3  
   system(sprintf('program.exe -i %d.txt -o output%d.txt -m 1',n,n));
end

(avoid using i and j as variables in MATLAB)

Upvotes: 3

am304
am304

Reputation: 13876

You need to change your syntax slightly:

for i=1:3
  input_file_name = [num2str(i),'.txt'];
  system(['program.exe -i ' input_file_name ' -o output_file1.txt -m 1 ']);
end

input_file_name is the name of your variable in MATLAB so you can write verbatim in the string you pass to the system command.

Upvotes: 6

Related Questions