No output on linux terminal when executing sytem or unix command of matlab

2 views (last 30 days)
Hi,
I am running MATLAB from a linux machine! Following is my problem
When I run any linux command in MATLAB using system or unix, it returns 0 as status but when I use the echo option in MATLAB or in terminal it doesnt display anything in the terminal!
I want to declare a variable in linux, assign it a value through matlab before running a func and then and after func executed change it to a different value!
cmds in matlab are:
% To set
[status] = system('export MYVAR1="1"')
[status] = system('export MYVAR1="1"','-echo')
[status] = unix('export MYVAR1="1"')
[status] = unix('export MYVAR1="1"','-echo')
%To view
[status] = system('echo $MYVAR1')
[status] = unix('echo $MYVAR1')
[status] = system('echo $MYVAR1', '-echo')
[status] = unix('echo $MYVAR1', '-echo')
In all cases status = 0 but neither it displays in the execution window nor on the terminal window I am running MATLAB from!
Regards,
MANAS

Accepted Answer

Walter Roberson
Walter Roberson on 24 Feb 2013
You cannot do that the way you are trying. Each system() or unix() call opens a new shell, does the operation in that shell, and then destroys the shell. Environment variables set in a shell only affect that shell and any subshells, and never any shells "upward".
What you should do is, in MATLAB,
setenv('MYVAR1', '1')
after which you can
getenv('MYVAR1')
This sets the the variable in the environment variables being held by the MATLAB process itself, and those variables then apply to any shells that MATLAB creates afterwards in the same proces, such as if you system() or unix()

More Answers (0)

Categories

Find more on Startup and Shutdown 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!