Function File: data = overwrite_struct(data_in, overwrite)
: = overwrite_struct(data_in, overwrite, options)

This function is useful for Octave programs which allow user input in form of octave expressions passed as command line arguments. Overwrite a value in a struct by an expression passed as a character string.

data_in … input data structure

overwrite … return value from overwrite_struct_add_arg

options.verbose … enable verbose output

ov.data.x = 1;
ov.data.y = 2;
args = {"data.x=1.5", "data.y=2.5"};
ovd = struct()([]);
for i=1:numel(args)
  ovd = overwrite_struct_add_arg(ovd, args{i});
endfor
ov = overwrite_struct(ov, ovd);

See also: overwrite_struct_add_arg.

Demonstration 1

The following code

 ov.data.x = 1;
 ov.data.opts.y = rand(3, 3);
 ov.data.opts.name.z = "abc";
 ovd = struct()([]);
 args = {"data.x=2", "data.opts.y=zeros(3, 3)", "data.opts.name.z=\"123\""};
 for i=1:numel(args)
   ovd = overwrite_struct_add_arg(ovd, args{i});
 endfor
 options.verbose = false;
 ov = overwrite_struct(ov, ovd, options);
 struct_print(ov);
 assert(ov.data.x, 2);
 assert(ov.data.opts.y, zeros(3, 3));
 assert(ov.data.opts.name.z, "123");

Produces the following output

ov.data.opts.name.z = "123";

ov.data.opts.y = [0,0,0;
                  0,0,0;
                  0,0,0];

ov.data.x = 2;

Package: mboct-octave-pkg