Is it possible to run terminal commands from matlab ??

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

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

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

So i have a question What i usually do 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 what i donot understand is what are the input vslue output value project directory and the cmd pattern

In what you describe,

projectdir = '~/palm/current_version'

and

cmd_pattern = 'Mrun -d "%s" > "%s" < "%s"');

This presumes that there is output that you want to save.

The first %s will get replaced with the name of the input file (which is located by looking in the directory for all files with the extension you have configured.)

The second %s will get replaced with the name of the output file, which is the same as the input file (to capture stdout in this case) but with a different extension that you have configured. In some commands you would not want to be capturing stdout and would want some command line option to indicate which file to write to.

The third %s will get replaced with the name of a temporary file that contains just "y", which is the input you indicated had to be given to confirm that you wanted to process the file. I/O redirection is used to send the "y" to the program.

So if i donot have an output i should not put the second %s i should remove from the code?
Also the input file arenot files that are inside of matlab directory. They are inside of the software directory. So simply just by writing the name of the input file in the Mrun command, it start working
You could change the output file name to '/dev/null'

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

3 Comments

I will check it tomorrow in the morning thanks
I am trying to run a shell from Matlab. The .sh file easily runs from the terminal, but not from Matlab. The error is:
line 9: fslroi: command not found.
It seems Matlab tries to run the whole shell file from Matlab, not from the system!
Any idea how to fix this issue?
Thanks in advance!
The issue is that when you start a shell from inside a process, then by definition it is not an "interactive shell" and so your .profile or equivalent is not executed. If your shell profile adds directories to the PATH environment variable, then those directories are not going to be on the path.
There are a few different workarounds:
  • add the PATH initialization to the system-wide shell profile. Traditionally that was /etc/profile but on MacOS you would instead need to edit a particular .plist
  • instead of having your command be directly system('fsutil etc') you can instead system('. ~/.profile; fsutil etc') where ~/.profile is a path to the profile to be sourced
  • or you could system('/path/to/fsutil etc')
  • or you could system('PATH=$PATH:/path/to/; fsutil etc')

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!