Source

boolkit / MatlabIO.ml

Full commit
(*** File IO methods ***)

let channel = ref (open_out "temp.temp")

(* returns a function to write strings to the channel *)
let open_file filename = 
   channel := open_out filename

let open_append filename = 
   channel := open_out_gen [Open_append] 777 filename

let close_file () = 
   close_out !channel

let print str = output_string !channel str

let show str = print (str ^ "\n")

(*** export methods ***)

let dblqte  = String.make 1 '\x22'
let snglqte = String.make 1 '\x27'

let bool_to_string b = 
   match b with
        true  -> "1"
      | false -> "0"

let string_of_string str = 
   snglqte ^ str ^ snglqte

let no_format x = x

let delimiter_list string_of_t delim li = 
   let rec delim_aux l = 
      match l with
           hd :: [] -> string_of_t hd
         | hd :: tl -> (string_of_t hd) ^ delim ^ (delim_aux tl)
         | [] -> ""
   in
      delim_aux li

let comma_list string_of_t = 
   delimiter_list string_of_t ", "
let semicolon_list string_of_t = 
   delimiter_list string_of_t "; "

let make_array_of_list string_of_t = 
   comma_list string_of_t

let t_list_to_array t_list_to_string t_list name = 
   name ^ " = [" ^ (t_list_to_string t_list) ^ "];\n"

let t_list_to_cell t_list_to_string t_list name = 
   name ^ " = {" ^ (t_list_to_string t_list) ^ "};\n"

(* returns a string "[1, 2, 3]" *)
let make_array_of_int_list    = make_array_of_list string_of_int
let make_array_of_float_list  = make_array_of_list string_of_float
let make_array_of_bool_list   = make_array_of_list bool_to_string
let make_array_of_string_list = make_array_of_list string_of_string

(* returns a string "name = [1, 2, 3];\n" *)
let    int_list_to_array = t_list_to_array make_array_of_int_list
let  float_list_to_array = t_list_to_array make_array_of_float_list
let   bool_list_to_array = t_list_to_array make_array_of_bool_list
let string_list_to_array = t_list_to_array make_array_of_string_list

(* returns a string "name = {1, 2, 3};\n" *)
let    int_list_to_cell = t_list_to_cell make_array_of_int_list
let  float_list_to_cell = t_list_to_cell make_array_of_float_list
let   bool_list_to_cell = t_list_to_cell make_array_of_bool_list
let string_list_to_cell = t_list_to_cell make_array_of_string_list

let make_array_of_float_matrix m name = 
   let lists = Array.create (Array.length m) "" in
   begin
      for i = 0 to Array.length m - 1 do
         lists.(i) <- comma_list (string_of_float) (Array.to_list m.(i))
      done;

      name ^ " = [" ^ (semicolon_list (no_format) (Array.to_list lists))
         ^ "];\n"
   end

let indexed_array name index = 
   name ^ "(" ^ (string_of_int index) ^ ")"