How to call a command prompt variable when executing operating system command from Matlab

3 次查看(过去 30 天)
Dear all,
I am trying to call a command prompt variable from within a operating system command using Matlab. An example of what I want to run using the command prompt:
set expr=$1=179*($1*0.265+$2*0.670+$3*0.065) & echo %expr%
Gets me the output I would like:
$1=179*($1*0.265+$2*0.670+$3*0.065)
When I try to run this from Matlab, using the following script:
cmd = 'set expr=$1=179*($1*0.265+$2*0.670+$3*0.065) & echo %expr%'
[status,cmdout] = system(cmd)
I get the following output:
cmdout =
'%expr%
'
Which suggests that matlab surrounds the %expr% in quotations before it sends the command to the command prompt.
The reason I am trying to do all this is because I want to use the string in expr as an input argument of a Radiance program which was written for Unix. The only way which I found I could do this on a Windows system is by first defining a variable containing the expression as a string and then calling this string from within the Radiance command argument.
I hope someone could shed some light on how I could solve this.
Kind regards,
Samuel
  3 个评论
Looky
Looky 2017-9-25
编辑:Looky 2017-9-25
Answering your main question. The reason that your code isn't working is that your set command executes, but the echo command doesn't see the new variable yet. This happens when commands are seperated by & and the time between setting and using the new variable is not long enough. You can get around this by using the cmd with the /V parameter like this (Windows only):
cmd='cmd /V:ON /c "set expr=179*($1*0.265+$2*0.670+$3*0.065) & echo %expr%"';
[status,cmdout] = system(cmd);
or use the setenv function of matlab to seperate setting and echoing of the variable.
Samuel de Vries
Samuel de Vries 2017-9-25
Thanks Looky!!
That indeed solved my problem. I wasn't aware of the fact that by default variables are only expanded once every line in the command prompt.
Kind regards,
Samuel

请先登录,再进行评论。

采纳的回答

Samuel de Vries
Samuel de Vries 2017-12-18
Question answered by Looky in the comments.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by