필터 지우기
필터 지우기

How to pass a Matlab variable to shell environment?

조회 수: 11 (최근 30일)
MatteP
MatteP 2018년 1월 15일
댓글: Walter Roberson 2023년 6월 19일
Good morning,
I have the same problem apperntly aksed in this thread
but not solved.
I need to pass a variable from matlab to UNIX shell. Let's say I want to print a variable defined within Matlab workspace using "echo" (let's skip for the moment why I would want to do it). This was my attempt
for i=1:9
! echo ${i}
end
but it does not work. How can I pass then a variable defined in Matlab to UNIX environment?
Thanks and best regards

답변 (1개)

Shivam
Shivam 2023년 6월 18일
You can use system command in matlab , here is one use case -
% Define variables
var1 = 'Hello';
var2 = 'world';
% Construct the command
command = ['echo ', var1, ' ', var2];
% Execute the command
system(command);
Output - Hello World
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 6월 19일
If the var* values contain shell meta characters, then this approach can end up a little different than the echo ${i} approach in shell. Best would be to add quotes, like
% Define variables
var1 = 'Hello';
var2 = 'world';
% Construct the command
command = "echo '" + var1 + "' '" + var2 + "'"
command = "echo 'Hello' 'world'"
% Execute the command
system(command);
Hello world

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by