Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Octave to change the variables in my input files?

Tags:

sed

octave

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?

like image 538
Reluctant_Linux_User Avatar asked May 01 '15 18:05

Reluctant_Linux_User


People also ask

How do you display a variable in Octave?

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.

How do you save a variable in Octave?

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.

Which of the following is used to read data from a file in Octave?

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.


2 Answers

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, sedwill 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).

like image 192
abarnert Avatar answered Oct 10 '22 14:10

abarnert


  1. 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.

  2. 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.

  3. 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).

like image 36
Ben Voigt Avatar answered Oct 10 '22 16:10

Ben Voigt