Fortran

From Octave
Revision as of 17:50, 29 November 2012 by Cdf (talk | contribs) (Created page with " {{Code|C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang="C" style="font-size:13px"> #include <octave_file_io.h> #include <oct...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Code: C++ function to load a matrix from an ASCII file in Octave native format
#include <octave_file_io.h>


#include <octave/oct.h>
#include <octave/ov.h>
#include <fstream>
#include <ls-oct-ascii.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/toplev.h>
#include <octave/load-save.h>
#include <octave/oct-map.h>
#include <cstring>

int octave_load (const char* filename, const char* varname, double** data, int* numel)
{
  
  string_vector argv (1);
  load_save_format format = LS_ASCII;

  install_types ();  
  argv(0) = varname;
  
  std::ios::openmode mode = std::ios::in | std::ios::binary;
  std::string fname (filename);

  std::ifstream file (fname.c_str (), mode);
  
  octave_scalar_map m = do_load (file, fname, format, oct_mach_info::flt_fmt_unknown,
                                 false, false, true, argv, 0, 1, 1).scalar_map_value ();

  file.close ();  

  Array<double> M = m.contents (varname).array_value ();
  if (error_state)
    return -1;

  *data = (double*) malloc (M.numel () * sizeof (double));
  *numel = M.numel ();
  std::cout << M << std::endl << *numel << std::endl;
  memcpy (*data, M.fortran_vec (), *numel * sizeof (double));

  return 0;  
}


Code: C interface to a function linking to liboctave
#ifndef OCTAVE_FILE_IO_H
#define OCTAVE_FILE_IO_H

#ifdef __cplusplus
extern "C" {
#endif
  
  int octave_load (const char* filename, const char* varname, double** data, int* numel);

#ifdef __cplusplus
}
#endif

#endif