MATLAB Answers

run a bash script from matlab

250 views (last 30 days)
Knut Jørgen
Knut Jørgen on 3 Oct 2014
Commented: Geoff Hayes on 31 Jan 2016
I have been trying to run a bash script from matlab with this code. The bash script use a program from Freesurfer. How can I pass arguments and enviroments argumnets from matlab to a bash script. I have set the enviroment both in bash and within matlab.
I use this code to start the bash script.
system(sprintf('subj2ascii_v2 %s',subject));
Geoff Hayes
Geoff Hayes on 31 Jan 2016
Carlo - how would you call 3dinfo from a terminal window? Just as
3dinfo ....
or as
./3dinfo ...
You may also want to specify the full path to your executable as
myExe = pathfullfile(pwd, Afni3dinfoLocation, '3dinfo');
and then using that from within your command.

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 3 Oct 2014
Knut - suppose that you have a bash script written as follows
echo Number of arguments passed: $#
for var in "$@"
echo "$var"
All the script does is accept a variable list of arguments and echoes each one to the console (or Command Window when run in MATLAB). Save the above to a file named and assign executable privileges to it (easiest way is to just run chmod 777 in a terminal or other window, in the directory that contains this file).
In MATLAB do the following
pathToScript = fullfile(pwd,''); % assumes script is in curent directory
subject1 = 'bert';
subject2 = 'ernie';
cmdStr = [pathToScript ' ' subject1 ' ' subject2];
The output from this script, as shown in the MATLAB Command Window, would be
Number of arguments passed: 2
  1 Comment
Walter Roberson
Walter Roberson on 31 Jan 2016
Better would be
cmdStr = sprintf('''%s'' ''%s'' ''%s''', pathToScript, subject, subject2);
This version quotes the path and the arguments in case they contain spaces or shell metacharacters.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!