Running UNIX shell commands in MATLAB
Show older comments
Hi, I'm trying to modify some MATLAB code that someone else has written (I don't really know MATLAB, just Python and FORTRAN) and am getting confused as to why some of the changes I made aren't doing what they are supposed to do. It has to do with running UNIX commands with MATLAB.
For example I run the command :
[nothing,tline] = unix(['echo ' ORG_STRUC.commandExecutable{POP_STRUC.POPULATION(Ind_No).Step} ' >> debugfile']);
and it writes the 'ORG_STRUC.command......" variable (which is just a short string) into my ASCII file called debugfile as expected. However, then I do the following:
[nothing, tline] = unix(['qsub ' ORG_STRUC.commandExecutable{POP_STRUC.POPULATION(Ind_No).Step} ]);
disp(tline);
disp(class(tline));
This submits a job to the supercomputer and returns a string into the variable tline, which is correctly printed by MATLAB when I type disp(tline) and it is of type CHAR as shown by disp(class(tline)). But, when I try to ECHO this variable out to a separate file as I did in the first section of code, like this
[nothing,rline] = unix(['echo ' tline ' >> debugfile']);
absolutely nothing is printed. How can this be? Am I missing something subtle? Thanks so much in advance for the help!
Answers (2)
Titus Edelhofer
on 30 May 2011
Hi,
strange indeed. Using the following MATLAB commands should do the same:
fid = fopen('debugfile', 'w');
if fid==-1
error('Could not open file.');
end
fprintf(fid, '%s\n', tline);
fclose(fid);
Titus
1 Comment
Walter Roberson
on 30 May 2011
It would have to be 'a' instead of 'w' to do the same thing as >>
Brad
on 30 May 2011
1 Comment
Walter Roberson
on 30 May 2011
Yes, that is a better route. If your tline happens to contain any shell meta-characters then your original version would not work.
Categories
Find more on Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!