Editing Fortran

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
This page describes an example of how to call liboctave functions from a Fortran program.
 
In the example we will load an Octave array from a file in Octave's native ASCII format,
 
it consists of two steps:
 
  
* write a C++ function with a C compatible interface and C linkage that reads a variable from an Octave ASCII file
 
* write Fortran code using the "iso_c_binding" intrinsic module to call the C++ function
 
  
=== C++ function ===
+
{{Code|C++ function to load a matrix from an ASCII file in Octave native format|<syntaxhighlight lang="C" style="font-size:13px">
 
 
{{Code|octave_file_io.cc: 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 <octave_file_io.h>
  
Line 48: Line 41:
 
   *data = (double*) malloc (M.numel () * sizeof (double));
 
   *data = (double*) malloc (M.numel () * sizeof (double));
 
   *numel = M.numel ();
 
   *numel = M.numel ();
 
+
  std::cout << M << std::endl << *numel << std::endl;
 
   memcpy (*data, M.fortran_vec (), *numel * sizeof (double));
 
   memcpy (*data, M.fortran_vec (), *numel * sizeof (double));
  
Line 56: Line 49:
  
  
=== Header file ===
+
{{Code|C interface to a function linking to liboctave|<syntaxhighlight lang="C" style="font-size:13px">
 
 
{{Code|octave_file_io.h: header file with C interface to octave_file_io.cc|<syntaxhighlight lang="C" style="font-size:13px">
 
 
#ifndef OCTAVE_FILE_IO_H
 
#ifndef OCTAVE_FILE_IO_H
 
#define OCTAVE_FILE_IO_H
 
#define OCTAVE_FILE_IO_H
Line 74: Line 65:
 
#endif
 
#endif
 
</syntaxhighlight>}}
 
</syntaxhighlight>}}
 
 
=== Fortran Code ===
 
 
{{Code|octave_file_io_example.f90|<syntaxhighlight lang="fortran" style="font-size:13px">
 
program octave_file_io_example
 
 
 
  use iso_c_binding
 
   
 
  implicit none
 
 
  interface
 
    function octave_load (filename, varname, data, numel) bind(c, name="octave_load")
 
     
 
      use iso_c_binding
 
      implicit none
 
 
      integer(c_int) :: octave_load
 
     
 
      character(kind=c_char), intent(in) :: filename(*)
 
      character(kind=c_char), intent(in) :: varname(*)
 
     
 
      type(c_ptr), intent(out) :: data
 
      integer(c_int), intent(out) :: numel
 
     
 
    end function octave_load
 
  end interface
 
 
 
  integer(c_int) :: res
 
  type(c_ptr) :: data
 
  real(c_double), pointer :: fdata(:)
 
  integer(c_int) :: numel
 
 
  res = octave_load (c_char_'pippo.octxt' // c_null_char, c_char_'A' // c_null_char, data, numel)
 
 
 
  call c_f_pointer (data, fdata, (/numel/))
 
 
 
  write (*,*) fdata
 
 
end program octave_file_io_example
 
</syntaxhighlight>}}
 
 
 
 
 
=== Compiling the code ===
 
 
  mkoctfile -I. octave_file_io.cc
 
  mkoctfile -I. --mkoctfile --link-stand-alone octave_file_io_example.f90 octave_file_io.o -o octave_file_io_example
 
 
[[Category:Examples]]
 

Please note that all contributions to Octave may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Octave:Copyrights for details). Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)

Templates used on this page: