How to pass a variable to a UNIX command, e.g. sed inside a matlab script?

2 次查看(过去 30 天)
I want to replace the value assigned to a parameter (say old) inside an FEA input file 'input_file' with a new value (say new) and save the modified file to 'output_file' with the following unix command.
for i=1:n
new = modulus(i)
!sed 's!\$ .*!'"old = new"'!' <input_file >output_file
end
This command is inside a *.m script file. It doesn't work and replaces the value of old with string 'new' and not the value of modulus(i). How can we pass a real variable to a unix command inside a matlab script?

回答(1 个)

José-Luis
José-Luis 2016-12-20
I don't think sed is integrated to Matlab so, yes, you'd need a system() call.
The power of regex is directly at your hands in Matlab for string manipulations.
But if all you want to do is change filenames, you don't really need sed. I would just use movefile().
  2 个评论
Arun Raina
Arun Raina 2016-12-20
The question is not about changing filenames, if you understand correctly. sed does work with matlab. In my output_file, the value of parameter old is always replaced but not by modulus(i).
José-Luis
José-Luis 2016-12-20
编辑:José-Luis 2016-12-20
I misunderstood what you meant.
My point still stands though. sed is not an integral part of Matlab. If you want to use that, then you could call it through system().
An alternative is to load the file, do the changes you need (e.g. regexp()) and save it under the new name.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by