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

2 Ansichten (letzte 30 Tage)
Arun Raina
Arun Raina am 20 Dez. 2016
Bearbeitet: José-Luis am 20 Dez. 2016
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?

Antworten (1)

José-Luis
José-Luis am 20 Dez. 2016
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 Kommentare
Arun Raina
Arun Raina am 20 Dez. 2016
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 am 20 Dez. 2016
Bearbeitet: José-Luis am 20 Dez. 2016
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by