MATLAB Answers

Is it possible to run terminal commands from matlab ??

107 views (last 30 days)
hello I am trying to run a command in terminal using Matlab
First,I need to change directory to a specific directory first using Matlab then I need run a command this command will be repeated 200 times but with different file names.
I do not want to keep entering the same command 200 times Not to mention when I enter the command to run the simulation, the terminals reply back asking me to confirm with y or n so I need to send 'y' for (yes) character too. is such thing possible or should I do it manually ?? I hope I get some guidance on what I need to search for exactly
thank you so much haggag
  1 Comment
mostafa haggag
mostafa haggag on 21 Apr 2018
What i usually do in Ubuntu I open terminal then i write an ssh command to connect to another pc and then i am asked for my password I change my home directory to the place the program is For example Cd palm/current_version Then i write the command Mrun - d filename........ (this line of Mrun is the one that need to be repeated many times) So

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Apr 2018
Edited: Walter Roberson on 20 Apr 2018
project_dir = '....';         %the place the files are
input_extension = 'dat';    %change as needed
output_extension = 'out';   %change as needed
cmd_pattern = '"C:\Program Files (x15)\Plan8\frobber.exe" -i "%s" -o "%s" < "%s"');
if ~exist(project_dir, 'dir')
  error('Input directory does not exist, "%s"', project_dir);
dinfo = dir( fullfile(project_dir, ['*.', input_extension] ) );
if isempty(dinfo);
  error('No files of appropriate type in "%s", project_dir);
file_names = {};
sayyes_file = [tempname() '.yes'];
fid = fopen(sayyes_file, 'wt');
if fid < 0
  error('Could not write temporary file: "%s"', sayyes_file);
fprintf(fid, 'y\n');
olddir= cd(project_dir);
for K = 1 : length(file_names)
  this_file = file_names{K};
  [~, basename, ~] = fileparts(this_file);
  outfilename = fullfile(project_dir, [basename '.' output_extension]);
  cmd = sprintf(cmd_pattern, this_file, outfilename, sayyes_file);
    [status, output] = system(cmd);
    if status ~= 0
      fprintf('something went wrong processing "%s", continuing\n', this_file);
  catch ME
     error('Hard failure processing "%s", giving up', this_file);

Sign in to comment.

More Answers (1)

Paul Shoemaker
Paul Shoemaker on 20 Apr 2018
Edited: Walter Roberson on 20 Apr 2018
Have you looked at the "system" and "dos" functions in Matlab? It sounds like they might do what you need.
Paul Shoemaker

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!