run a bash script from matlab

조회 수: 121 (최근 30일)
Knut Jørgen
Knut Jørgen 2014년 10월 3일
댓글: Geoff Hayes 2016년 1월 31일
Hi
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.
subject='bert';
system(sprintf('subj2ascii_v2 %s',subject));
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2016년 1월 31일
Carlo Iaboni's answer moved here
Hello I tried everything. Can you see what I'm doing wrong?
contents of GO.m ----------------
cd('/Volumes/SSD/MATLAB/NIH_toolbox/MVPA');
saveDir = eval('pwd');
Afni3dinfoLocation = '/Volumes/Macintosh HD/abin';
cd(Afni3dinfoLocation);
dir 3dinfo
fileName = '/Volumes/SSD/MATLAB/NIH_toolbox/MVPA/COP19/STATS/3COMB_RUNS/ANATICOR/s19.TASK_c01a.GLM_ANATICOR_REML+orig.BRIK';
cmd=sprintf('3dinfo %s',fileName);
eval(sprintf('!3dinfo %s',fileName));
err=system(sprintf('3dinfo %s',fileName));
system('ls 3dinfo');
system(cmd);
display(cmd);
cd(saveDir);
------------------ results in:
>> GO
3dinfo
/bin/bash: 3dinfo: command not found
/bin/bash: 3dinfo: command not found
3dinfo
/bin/bash: 3dinfo: command not found
cmd =
3dinfo /Volumes/SSD/MATLAB/NIH_toolbox/MVPA/COP19/STATS/3COMB_RUNS/ANATICOR/s19.TASK_c01a.GLM_ANATICOR_REML+orig.BRIK
Geoff Hayes
Geoff Hayes 2016년 1월 31일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Geoff Hayes
Geoff Hayes 2014년 10월 3일
Knut - suppose that you have a bash script written as follows
#!/bin/bash
args=("$@")
echo Number of arguments passed: $#
for var in "$@"
do
echo "$var"
done
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 myBashScript.sh and assign executable privileges to it (easiest way is to just run chmod 777 myBashScript.sh in a terminal or other window, in the directory that contains this file).
In MATLAB do the following
pathToScript = fullfile(pwd,'myBashScript.sh'); % assumes script is in curent directory
subject1 = 'bert';
subject2 = 'ernie';
cmdStr = [pathToScript ' ' subject1 ' ' subject2];
system(cmdStr);
The output from this script, as shown in the MATLAB Command Window, would be
Number of arguments passed: 2
bert
ernie
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 1월 31일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by