Can not run system commands from matlab -MAC OSX
이전 댓글 표시
I am trying to run a image processing software called Flirt through matlab. When I try
system'flirt', I get a /bin/bash: flirt: command not found error.
If I try system('/usr/local/fsl/bin/flirt'); then it works fine. Typing just flirt in terminal also launches the program.
Is there a way of setting matlab to find this program in path and running it without giving its full address?
Thanks
Hasan
답변 (1개)
Matthias Fripp
2017년 10월 8일
For some reason, Matlab on a Mac doesn't see the whole search path you normally get in a terminal. So you need to add the directory to the $PATH environment variable within Matlab. You can do that temporarily by running
setenv('PATH', [getenv('PATH') ':/usr/local/fsl/bin']);
You can make this permanent by adding the command to a startup.m file somewhere in your Matlab path (as shown by Matlab's "path" command).
댓글 수: 10
Walter Roberson
2017년 10월 8일
"For some reason, Matlab on a Mac doesn't see the whole search path you normally get in a terminal."
That is misleading.
Oni:358789 roberson$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin
Oni:358789 roberson$ matlab -nodesktop -nojvm
< M A T L A B (R) >
Copyright 1984-2017 The MathWorks, Inc.
R2017a (9.2.0.538062) 64-bit (maci64)
[...]
>> getenv('PATH')
ans =
'/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin'
So if you start MATLAB from a terminal, you will get the PATH inherited from the shell.
The issue is that when you click on the icon in MacOS or OS-X, you are not running at the terminal: the program is run by launcher, which does not go through a shell login session. You get the system default PATH environment variable.
I have posted more detailed explanations before, including information about which shell initialization files are or are not executed when you system() on OS-X / MacOS / Linux systems, and I have posted links to information on how to permanently modify the system default PATH .
Valentin Ruano-Rubio
2020년 1월 21일
편집: Valentin Ruano-Rubio
2020년 1월 21일
@WalterRoberson
"I have posted more detailed explanations before, including information about which shell initialization files are or are not executed when you system() on OS-X / MacOS / Linux systems, and I have posted links to information on how to permanently modify the system default PATH ."
Thanks, this is very helpful, however could you please add links to such posts? or perhaps alternative reference if you know one.
Thanks, V.
Walter Roberson
2020년 1월 21일
/etc/profile or ~/.bash_profile or ~/.bash_login or ~/.profile are only checked for interactive login, which would not be the case in the situation of invoking MATLAB from a graphical launcher. However, ~/.bashrc would be used for interactive non-login shells, and for any shell the environment variable BASE_ENV is checked and if it refers to a file then the file is sourced. But if the shell was invoked as "sh" instead of as "bash" then some of the above files are skipped and the environment variable examined is ENV instead of BASE_ENV
I linked above to information on how to set an environment variable through a .plist file on Mac.
Caution: increasingly in MacOS, the LD_ENVIRONMENT_PATH variable is ignored for applications in /Applications . The Apple documentation implies that the restriction should only be for pre-installed Apple products, but the practice is quite different: people are finding that MATLAB (and many other programs) are affected. The work-around is typically to put links to libraries into /usr/lib so that they will be trusted.
Cristiana Dinea
2023년 2월 25일
편집: Cristiana Dinea
2023년 2월 25일
@Walter Roberson hi,I did what you reccomended above and I am still unable to run fsl commands inside matlab terminal.
>> imglob
Unrecognized function or variable 'imglob'.
Walter Roberson
2023년 2월 25일
Setting the environment variables correctly has to do with what executables can be found when you use system() or the ! command in MATLAB. It does not provide a way for MATLAB to find .m or .mdl or .slx or similar -- use pathtool for that. It also does not provide a way to be able to execute external programs such as randomize or imglob from the MATLAB command line -- you have to system() or ! to run those.
Cristiana Dinea
2023년 2월 25일
@Walter Roberson thank you for the reply.
I am runnign this script fro converting dicom to nifti
function par = dicom2nii(src, outfile)
%% Converting DICOM to NIFTI for 4D data
%
% Syntax:
% [filename, par] = dicom2nii(src, [options])
%
% Description:
% This function is an extension of the DICOM-to_NIFTI converter
% 'dicm2nii'. It converts a set of DICOM files to NIFTI format
% for 4D data. It supports the automatic conversion to a 4D NIFTI
% file.
%
% Input Arguments:
% src - the directory where DICOM files are located.
% outfile - the output NIFTI file.
%
% Output Arguments:
% par - a structure of DICOM parameters.
%
% Examples:
% dicom2nii('dcm','out.nii.gz');
%
% See also
%
% References:
%
% Notes:
% 1. This function requires the DICOM-to_NIFTI converter dicm2nii.
% https://www.mathworks.com/matlabcentral/fileexchange/42997-dicom-to-nifti-converter--nifti-tool-and-viewer
% 2. This function requires FSL, and works on Linux.
%
% History:
% 07/14/2016 - initial script.
%
%__________________________________________________________________________
% Wonsang You(wsgyou@gmail.com)
% 07/14/2016
% Copyright (c) 2016 Wonsang You.
%% converting DCM to NIFTI for each volume
path_str = fileparts('out.nii.gz');
niidir = fullfile(path_str,'nii');
subject = dicm2nii('/Users/cdinea/Downloads', niidir, '3D.nii.gz');
data = load(fullfile(niidir,'dcmHeaders.mat'));
prefix = fieldnames(data.h);
%% concatenating 3D volumes into a 4D NIFTI files
system(sprintf('full_list=`imglob %s_?????.*`;fslmerge -t %s $full_list',fullfile(niidir,prefix{1}),'out.nii.gz'),'-echo');
system(sprintf('rm -f %s',niidir));
Cristiana Dinea
2023년 2월 25일
so imglob is running with the script.For some reason I still get
>> dicom2nii
Validating 21396 files ... (1615 valid)
Converting 1 series (GE) into 3-D .nii.gz: subject '***'
x5_95_5_1_25MM 512x512x85x19
Elapsed time by dicm2nii is 132.9 seconds
zsh:1: command not found: imglob
zsh:1: command not found: fslmerge
rm: nii: is a directory
Walter Roberson
2023년 2월 25일
What steps did you take to put imglob and fslmerge onto your shell PATH? And what is the path to each of those executables?
Cristiana Dinea
2023년 3월 8일
@Walter Roberson I set
>> setenv('PATH', [getenv('PATH') ':/Users/cdinea/fsl/bin']);
and on my Mac fsl and imglob are recognized
cdinea@cdinea-mlt bin % imglob
Usage: imglob [-extension/extensions] <list of names>
-extension for one image with full extension
-extensions for image list with full extensions
카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!