MATLAB Answers

Is it possible to run terminal commands from matlab ??

156 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);
end
dinfo = dir( fullfile(project_dir, ['*.', input_extension] ) );
if isempty(dinfo);
  error('No files of appropriate type in "%s", project_dir);
end
file_names = {dinfo.name};
sayyes_file = [tempname() '.yes'];
fid = fopen(sayyes_file, 'wt');
if fid < 0
  error('Could not write temporary file: "%s"', sayyes_file);
end
fprintf(fid, 'y\n');
fclose(fid);
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);
  try
    [status, output] = system(cmd);
    if status ~= 0
      fprintf('something went wrong processing "%s", continuing\n', this_file);
    end
  catch ME
     cd(olddir);
     error('Hard failure processing "%s", giving up', this_file);
  end
end
cd(olddir);

  7 Comments

Show 4 older comments
Walter Roberson
Walter Roberson on 21 Apr 2018
You could change the output file name to '/dev/null'
mostafa haggag
mostafa haggag on 21 Apr 2018

so what i do in the terminal is the following ssh -X xxxxxxxx@xxxxx.xxx.xxxxxx.com (i replaced some personal data to xx in hre) then the pc asks for my password of my account so i enter it now i I am using a terminal of the other computer i change the directory of the other computer to cd palm /currentversion then i run this mrun -d example neutral -b -h xxxxx -K parallel -X 48 -T 24 -t 1800 -q xxxx.p -r "d3# ts# pr# xy#"

I wanted to know how can i add the part ssh and the password at the beginning of the code ???

sorry if i am repeating the explanation i am just trying to be as clear as possible example neutral is the name of the folder inside of the palm directory.

if I understand correctly it should be like this cmd_pattern = 'mrun -d example "%s -b -h xxxxx -K parallel -X 48 -T 24 -t 1800 -q xxxx.p -r "d3# ts# pr# xy#" > "%s" < "%s"');

also cmd_pattern = 'Mrun -d "%s" > "%s" < "%s"'); should i remove the %s for the output or dev/null will do the thing ? fprintf(fid, 'y\n'); also how does this chooses y instead of no ?? thank you sooo sooo much you are a life saver yours haggag

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

  1 Comment

mostafa haggag
mostafa haggag on 21 Apr 2018
I will check it tomorrow in the morning thanks

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!