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?
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!