How to pass a Matlab variable to shell environment?
16 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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
Learn More about system commands - Execute operating system command and return output - MATLAB system - MathWorks India
1 个评论
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 + "'"
% Execute the command
system(command);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!