Running UNIX shell commands in MATLAB

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)

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

It would have to be 'a' instead of 'w' to do the same thing as >>

Sign in to comment.

Well, I tried the following thing and it worked. Maybe adding some extra quotations "" fixed it....
command = sprintf('%s%s%s','echo "',tline,'" >> debugfile');
disp(command);
[nothing,rline] = unix(command)
Thanks so much for everyone who responded! =)

1 Comment

Yes, that is a better route. If your tline happens to contain any shell meta-characters then your original version would not work.

Sign in to comment.

Categories

Find more on Functions in Help Center and File Exchange

Tags

Asked:

on 29 May 2011

Community Treasure Hunt

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

Start Hunting!