Octave load: Difference between revisions

From Octave
Jump to navigation Jump to search
No edit summary
(Mark as outdated.)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Warning|This article is '''outdated'''.  For current C++ code examples see the Octave Manual https://octave.org/doc/latest/Standalone-Programs.html.}}


The code below shows an example of how to load a matrix from a file in Octave's binary file format.
The code below shows an example of how to load a matrix from a file in Octave's binary file format.
An example of how to load data from a file in Octave's ascii format can be found in this page [[fortran|page]].
An example of how to load data from a file in Octave's ascii format can be found in this [[fortran|page]].


{{Code|octave_binary_io_example.cc: C++ function to load a matrix from a BINARY file in Octave native format|<syntaxhighlight lang="C" style="font-size:13px">
{{Code|octave_binary_io_example.cc: C++ function to load a matrix from a BINARY file in Octave native format|<syntaxhighlight lang="C" style="font-size:13px">
Line 19: Line 20:
std::fstream file;
std::fstream file;
std::ios::openmode m = std::ios::in;
std::ios::openmode m = std::ios::in;
bool check_gzip_magic (const std::string& fname);
bool fexists (const std::string& fname);
load_save_format format = LS_BINARY;
load_save_format format = LS_BINARY;
oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown;
oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown;
Line 27: Line 26:
int main (void)
int main (void)
{
{
  int mode = 1;
  double **data;
  int *rank;
  int **shape;
   string_vector argv (1);
   string_vector argv (1);


Line 72: Line 67:


$ mkoctfile --link-stand-alone octave_binary_io_example.cc
$ mkoctfile --link-stand-alone octave_binary_io_example.cc
$ ./a.ut
$ ./a.out


</syntaxhighlight>}}
</syntaxhighlight>}}
[[Category:Outdated pages]]

Latest revision as of 06:07, 13 July 2020

Warning icon.svg
This article is outdated. For current C++ code examples see the Octave Manual https://octave.org/doc/latest/Standalone-Programs.html.

The code below shows an example of how to load a matrix from a file in Octave's binary file format. An example of how to load data from a file in Octave's ascii format can be found in this page.

Code: octave_binary_io_example.cc: C++ function to load a matrix from a BINARY file in Octave native format
#include <fstream>
#include <octave/oct.h>
#include <octave/ov.h>
#include <octave/zfstream.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/toplev.h>
#include <octave/load-save.h>
#include <octave/ls-oct-binary.h>
#include <octave/oct-map.h>
#include <cstring>

std::fstream file;
std::ios::openmode m = std::ios::in;
load_save_format format = LS_BINARY;
oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown;
bool swap = false;

int main (void)
{
  string_vector argv (1);

  install_types ();  
  argv(0) = std::string ("test_var");

  file.open ("test.bin", m);
  if (read_binary_file_header (file, swap, flt_fmt, true) != 0)
    return -1;

  octave_scalar_map m = do_load (file, "test.bin", format, 
				 flt_fmt, false, swap, true, argv, 
				 0, 1, 1).scalar_map_value ();


  std::cout << m.contents ("test_var").matrix_value ();

  file.close ();
  return 0;

}


To test, type the following in Octave:

>> test_var = randn(5);
>> save -binary test.bin test_var


Compile and run the example with the following commads

$ mkoctfile --link-stand-alone octave_binary_io_example.cc
$ ./a.out