1,852
edits
Carandraug (talk | contribs) (fix name of fortran file) |
(→C++ function: Update function to work with Octave 5.2.0+) |
||
Line 8: | Line 8: | ||
=== C++ function === | === C++ function === | ||
{{Code|octave_file_io.cc: C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang=" | {{Code|octave_file_io.cc: C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang="CC"> | ||
// Octave header | |||
#include <octave/oct.h> | |||
#include <octave/ls-mat-ascii.h> | |||
// Custom header containing the C compatible interface | |||
#include <octave_file_io.h> | #include <octave_file_io.h> | ||
//! Load a single matrix, stored in ASCII format, from a data file. | |||
//! | |||
//! @param file_name name of the data file. | |||
//! @param data pointer to the read-in matrix stored as fortran vector | |||
//! (column-major order). | |||
//! @param numel number of elements in @p data. | |||
int octave_load (const char* file_name, double** data, int* numel) | |||
int octave_load (const char* | |||
{ | { | ||
// Define variable to hold the read data. | |||
octave_value read_data; | |||
// Read a plain ASCII matrix from data file. | |||
std::ifstream in_file_stream (file_name, std::ios::binary); | |||
read_mat_ascii_data (in_file_stream, file_name, read_data); | |||
std:: | in_file_stream.close (); | ||
// Convert read data to numerical array (matrix). | |||
NDArray A = read_data.array_value (); | |||
// Extract number of elements in matrix A. | |||
*numel = A.numel (); | |||
// Allocate memory to pointer to returned values. | |||
*data = (double*) malloc (A.numel () * sizeof (double)); | |||
*data | // Copy the content of matrix A to data structure Fortran can handle. | ||
memcpy (*data, A.fortran_vec (), A.numel () * sizeof (double)); | |||
return 0; | |||
return 0; | |||
} | } | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
=== Header file === | === Header file === |