Why do SYSTEM commands (like LS) not function as expected when I call them from MATLAB 7.11 (R2010b) ?

11 views (last 30 days)
I use the LS command to obtain a list of all the MAT files in a directory and its subdirectories. When I put them in a string, I notice that the first few lines that are displayed are garbage.
A similar unexpected behavior occurs when I execute other linux commands from MATLAB using the SYSTEM command.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Apr 2013
Edited: Edric Ellis on 13 Dec 2018
This behavior is due to interactions between MATLAB system command and underlying Linux shell. In particular, some terminal settings could lead to additional characters being returned by the call to the Unix shell.
To work around this issue try the following within MATLAB:
Before issuing any commands on the MATLAB command prompt:
1) Set MATLAB_SHELL environment variable to /bin/sh:
>> setenv('MATLAB_SHELL','/bin/sh');
2) Test the command that is causing the error.
3) Modify your .bashrc/.cshrc files to set MATLAB_SHELL to /bin/sh
Note: it is possible for step 1 to not fix the issue on its own. If just setting MATLAB_SHELL within MATLAB does not resolve the issue:
1) Shut down MATLAB
2) Modify your .bashrc/.cshrc files to set MATLAB_SHELL to /bin/sh
3) Open a new Linux terminal
4) Start MATLAB
5) verify whether you get correct output
If neither approaches resolve the issue, and you are issuing custom Linux commands via the SYSTEM call or the ! operator try the following:
If your command takes the format:
>> [a, b] = system('foo')
replace it with
>> [a, b] = system('foo < /dev/null')
Using the < /dev/null redirect will disable interactive input from MATLAB and should remove character corruption from the output.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2010b

Community Treasure Hunt

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

Start Hunting!