I am trying to get Octave to change variables in my input files like it would if I were using the command line in Ubuntu. I've stripped this back to a simple case where I get it to change '1111' to the string Naer_str and '2222' to the string sigma_str; printing the result to a new file. 1111 and 2222 are both present in the first file. Here is the entire code I am running below.
Naer_str= num2str(1000)
disp(Naer_str)
sigma_str = num2str(0.491)
eval(['system(''sed -e "s~1111~${Naer_str}~; s~2222~${sigma_str}~;" OctaveChangeVarTestFile.IN > OctaveChangeVarTestFile_out.IN'');']);
The new input file is made, but rather than seeing the values 1000 and 0.491 as I anticipated the places where 0.491 and 1000 should be are blank. Running it in debug mode showed the follow error message (which I've seen a lot since I started to use Octave a few days ago, but I still don't really grasp what it means).
error: invalid use of script in index expression
I have tried just inputting stuff into the Linux terminal like so:
Naer_str=1000
sigma_Str=0.491
sed -e "s/1111/${Naer_str}/; s/2222/${sigma_str}/;" OctaveChangeVarTestFile.IN > OctaveChangeVarTestFile_out.IN
This appears to work fine, so I assume that I must have made a grammar error either using system or eval, but I can't see it.
What is causing this error message? Why am I not seeing my anticipated output?
To print the value of a variable without printing its name, use the function disp . The format command offers some control over the way Octave prints values with disp and through the normal echoing mechanism.
Command: save options file -struct STRUCT f1 f2 … Save the named variables v1 , v2 , …, in the file file . The special filename ' - ' may be used to write output to the terminal. If no variable names are listed, Octave saves all the variables in the current scope.
If the data file contains only numbers (TAB- or space-delimited columns), a matrix of values is returned. Otherwise, load returns a structure with members corresponding to the names of the variables in the file. The load command can read data stored in Octave's text and binary formats, and MATLAB's binary format.
I don't know why specifically you're getting this error, but I'm 90% sure this won't do what you want:
eval(['system(''sed -e "s~1111~${Naer_str}~; s~2222~${sigma_str}~;" OctaveChangeVarTestFile.IN > OctaveChangeVarTestFile_out.IN'');']);
In bash, ${Naer_str}
inside a double-quoted string is a variable substitution: it replaces the ${Naer_str}
with the value of the Naer_str
variable in the string, before passing it along to the command. So, sed
will get s/1111/1000/;
as one of its commands.
But I can't find any reference to such a feature in octave's Variables, Manipulating Strings, Strings, or elsewhere. Those characters are just going to be treated as literal characters. So, sed
will get s~1000~${Naer_str}~;
as an argument, which means it's going to produce a garbage script. It looks like the way you do this kind of thing in octave is by using printf-style formatting (as described all over the Manipulating Strings page).
It looks like all you have is a system
call with a computed command. You can pass a computation result as the parameter of system()
already, you don't need, and should not use, eval()
here.
The way to concatenate strings (character arrays) in octave is just horizontal matrix concatenation. So instead of 's~1111~${Naer_str}~'
(which as abarnert points out, gets interpolated by the shell which has no concept of octave variables) you can write ['s~1111~' Naer_str]
and octave will concatenate.
You don't need to reach outside octave to sed
via system()
for text replacement. strrep
and regexprep
can do replacement on character arrays right inside octave (or Matlab).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With