Applying Matlab variables in a UNIX command

I am building a MATLAB script to generate many input files with unique file names, then to feed these files to a program that can only be run from the command line (UNIX, I'm on a Mac). One of my final barriers is to be able to use MATLAB variables in the UNIX command line. This is a very basic example, but for instance if I execute the following code:
folderName = test;
unix('cd /path; mkdir folderName')
the folder is created with the name "folderName" rather than test, whereas I was hoping for the path /path/test to be created. Any ideas?
Thank you very much!

답변 (2개)

Thorsten
Thorsten 2016년 1월 18일
편집: Thorsten 2016년 1월 18일

0 개 추천

You can create the folder in Matlab as follows:
path = '/User/foobar/Matlab/xxx';
folder = 'test';
mkdir(fullfile(path, folder))

댓글 수: 2

Kevin
Kevin 2016년 1월 18일
편집: Kevin 2016년 1월 18일
Thank you for your reply. It's clear to me that I did a poor job in asking this question, but it's important that I run in the UNIX environment.
I have a program that only runs from the command line, and I'm giving it input variables in the filename used in the command that are variables in the Matlab script. If I use UNIX, the command would look like
Program filename_var1_var2.txt
Etc, etc. Any ideas? Thank you.
Thorsten
Thorsten 2016년 1월 19일
편집: Thorsten 2016년 1월 19일
You have to write the text file filename_var1_var2.txt from Matlab, and then call your program from the command line or from Matlab using system. What's the format of filename_var1_var2.txt? Do you have problems generating this file from Matlab? Have a look at Walter's answer that shows the principle way of how to do it.

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

Walter Roberson
Walter Roberson 2016년 1월 18일

0 개 추천

folderName = 'test';
unix( sprintf('cd /path; mkdir ''%s''', folderName) )
If you want to live dangerously:
folderName = 'test';
unix( ['cd /path; mkdir ', folderName] )
which would lead you to endless fun if the folderName contained any shell special characters.

댓글 수: 4

for K = 1 : 20
%generate temporary file names
this_infile = tempname();
this_outfile = tempname();
%create some input data
fid = fopen(this_infile, 'w');
fprintf(fid, '%f ', rand(1,10) );
fprintf(fid, '\n');
fclose(fid);
%build the command
cmd = sprintf('YourProgram ''%s'' >> ''%s''', this_infile, this_outfile);
%run the command
[status, cmd_output] = system(cmd);
%read the output file
fid = fopen(this_outfile, 'r');
results = fscanf(fid, '%f', 1);
fclose(fid);
fprintf('Run #%K responded with: %f\n', results);
%get rid of the files
delete(this_infile);
delete(this_outfile);
end
Kevin
Kevin 2016년 1월 20일
편집: Kevin 2016년 1월 20일
Hi Walter,
Thank you for the reply. I've tried to implement your suggestion but have ran in to more issues with my program. I am trying to use a program called SIMPSON from a variable directory as determined in the first unix command. When I run the following code:
function s=SimpExecute(simpsonInput)
cmd = 'cd /Users/kevin/Desktop/Sims/PulseSims;simpson simpsonInput';
[status,cmd_output] = unix(cmd)
end
I get the results:
status = 127
cmd_output = /bin/bash: simpson: command not found
Strangely, when I run the identical command in the Terminal command line, SIMPSON runs perfectly. Perhaps the path is misset? However when I type
whereis simpson
in Terminal, it outputs no results.
I have re-run the top code, but instead of trying to run SIMPSON I asked it to echo the path of the environment after changing directories
function s=SimpExecute(simpsonInput)
cmd = 'cd Desktop/Sims/PulseSims;echo $PATH';
[status,cmd_output] = unix(cmd)
end
and receive this is output:
status =
0
cmd_output =
/usr/bin:/bin:/usr/sbin:/sbin
I'm at a loss. Do you have any suggestions? Thank you in advance for your help. Cheers.
Your PATH does not include your current directory. You could add that to the PATH, or you could change your command to
cmd = 'cd /Users/kevin/Desktop/Sims/PulseSims;./simpson simpsonInput';
Something in your program must have reset your PATH, or else you have started MATLAB in an odd way. For me on OS-X, starting by clicking the icon in the dock, I get
/Applications/MATLAB_R2014a.app/sys/java/jre/maci64/jre/bin:/Library/Frameworks/Maple.framework/Versions/2016/bin.APPLE_UNIVERSAL_OSX:/Library/Frameworks/Maple.framework/Versions/2016/jre.APPLE_UNIVERSAL_OSX/bin/:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/MATLAB_R2014a.app/bin:/Applications/MATLAB_R2014a.app/bin/maci:/Applications/MATLAB_R2014a.app/sys/os/maci:/Applications/MATLAB_R2014a.app/bin/maci64:/Applications/MATLAB_R2014a.app/sys/os/maci64
If you are starting MATLAB from the command line on a Linux system then you would be invoking a shell script named "matlab" which should set the PATH and then invoke the MATLAB executable. It is not impossible that someone has mangled that script.
The only other reason I can think of that your PATH might be like that is if someone has changed the actual MATLAB executable to be suid or sguid : in such a case in order to prevent security problems, the operating system would automatically change to a restricted PATH very similar to the one you are seeing, ignoring any environment variables.
Walter,
Thank you again for your reply. I tried your suggestions, and while they did not work exactly, your comment got me thinking. In the end I only had to add a bit to PATH:
PATH=$PATH:/bin:/usr/local/bin
and it was able to find the command simpson. Thanks so much for your help and inspiration!

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

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

질문:

2016년 1월 18일

댓글:

2016년 1월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by