/* $Revision: 1.5 $ */
/*
 * MAT-file creation program
 *
 * See the MATLAB API Guide for compiling information.
 *
 * Calling syntax:
 *
 *   matcreat
 *
 * Create a MAT-file which can be loaded into MATLAB.
 *
 * This program demonstrates the use of the following functions:
 *
 *  matClose
 *  matGetArray
 *  matOpen
 *  matPutArray
 *  matPutArrayAsGlobal
 *
 * Copyright (c) 1984-98 by The MathWorks, Inc.
 * All Rights Reserved.
 */

#include <stdio.h>
#include "mat.h"

#define BUFSIZE 255

int create(const char *file) {
  MATFile *pmat;
  mxArray *pa1, *pa2, *pa3;
  double data[9] = { 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 };
  char str[BUFSIZE];

  printf("Creating file %s...\n\n", file);
  pmat = matOpen(file, "w");
  if (pmat == NULL) {
    printf("Error creating file %s\n", file);
    printf("(do you have write permission in this directory?)\n");
    return(1);
  }

  pa1 = mxCreateDoubleMatrix(3,3,mxREAL);
  mxSetName(pa1, "LocalDouble");

  pa2 = mxCreateDoubleMatrix(3,3,mxREAL);
  mxSetName(pa2, "GlobalDouble");
  memcpy((char *)(mxGetPr(pa1)), (char *)data, 3*3*sizeof(double));
  
  pa3 = mxCreateString("MATLAB: the language of technical computing");
  mxSetName(pa3, "LocalString");

  matPutArray(pmat, pa1);
  matPutArrayAsGlobal(pmat, pa2);
  matPutArray(pmat, pa3);

  /*
   * Ooops! we need to copy data before writing the array.  (Well,
   * ok, this was really intentional.) This demonstrates that
   * matPutArray will overwrite an existing array in a MAT-file.
   */
  memcpy((char *)(mxGetPr(pa1)), (char *)data, 3*3*sizeof(double));
  matPutArray(pmat, pa1);

  /* clean up */
  mxDestroyArray(pa1);
  mxDestroyArray(pa2);
  mxDestroyArray(pa3);

  if (matClose(pmat) != 0) {
    printf("Error closing file %s\n",file);
    return(1);
  }

  /*
   * Re-open file and verify its contents with matGetArray
   */
  pmat = matOpen(file, "r");
  if (pmat == NULL) {
    printf("Error reopening file %s\n", file);
    return(1);
  }

  /*
   * Read in each array we just wrote
   */
  pa1 = matGetArray(pmat, "LocalDouble");
  if (pa1 == NULL) {
    printf("Error reading existing matrix LocalDouble\n");
    return(1);
  }
  if (mxGetNumberOfDimensions(pa1) != 2) {
    printf("Error saving matrix: result does not have two dimensions\n");
    return(1);
  }

  pa2 = matGetArray(pmat, "GlobalDouble");
  if (pa2 == NULL) {
    printf("Error reading existing matrix GlobalDouble\n");
    return(1);
  }
  if (!(mxIsFromGlobalWS(pa2))) {
    printf("Error saving global matrix: result is not global\n");
    return(1);
  }

  pa3 = matGetArray(pmat, "LocalString");
  if (pa3 == NULL) {
    printf("Error reading existing matrix LocalDouble\n");
    return(1);
  }
  mxGetString(pa3, str, 255);
  if (strcmp(str, "MATLAB: the language of technical computing")) {
    printf("Error saving string: result has incorrect contents\n");
    return(1);
  }

  /* clean up before exit */
  mxDestroyArray(pa1);
  mxDestroyArray(pa2);
  mxDestroyArray(pa3);

  if (matClose(pmat) != 0) {
    printf("Error closing file %s\n",file);
    return(1);
  }
  printf("Done\n");
  return(0);
}

int main()
{ 
  int result;
 
  result = create("mattest.mat");
  return (result==0)?EXIT_SUCCESS:EXIT_FAILURE;
}
