Skip to content

Instantly share code, notes, and snippets.

@sgqy
Last active November 29, 2018 10:06
Show Gist options
  • Save sgqy/2721f1133706d1b3fb5988b07d18f61e to your computer and use it in GitHub Desktop.
Save sgqy/2721f1133706d1b3fb5988b07d18f61e to your computer and use it in GitHub Desktop.
#include "mat.h" // from MATLAB
#include "matrix.h" // from MATLAB
#include "matio.h" // https://github.com/tbeu/matio
#include "string.h"
#include "stdlib.h"
/*
* Open a MAT-file "filename" using mode "mode". Return
* a pointer to a MATFile for use with other MAT API functions.
*
* Current valid entries for "mode" are
* "r" == read only.
* "w" == write only (deletes any existing file with name <filename>).
* "w4" == as "w", but create a MATLAB 4.0 MAT-file.
* "w7.3" == as "w", but create a MATLAB 7.3 MAT-file.
* "u" == update. Read and write allowed, existing file is not deleted.
*
* Return NULL if an error occurs.
*/
MATFile* matOpen(const char *filename, const char * mode)
{
int imode = 0;
if(strcmp(mode, "r") == 0)
{
imode |= MAT_ACC_RDONLY;
imode |= MAT_FT_UNDEFINED;
}
if(strcmp(mode, "w") == 0 || strcmp(mode, "u") == 0)
{
imode |= MAT_ACC_RDWR;
imode |= MAT_FT_UNDEFINED;
}
if(strcmp(mode, "w4") == 0)
{
imode |= MAT_ACC_RDWR;
imode |= MAT_FT_MAT4;
}
if(strcmp(mode, "w7.3") == 0)
{
printf("fatal: %s is not supported\n", filename);
abort();
}
return reinterpret_cast<MATFile*>(
Mat_Open(filename, imode)
);
}
/*
* Close a MAT-file opened with matOpen.
* The pointer-to-MATfile argument is invalid, once matClose returns.
* Return zero for success, EOF on error.
*/
matError matClose(MATFile *pMF)
{
return Mat_Close(
reinterpret_cast<mat_t*>(pMF)
);
}
/*
* Read the array value for the specified variable name from a MAT-file.
*
* Return NULL if an error occurs.
*/
mxArray * matGetVariable(MATFile * pMF, const char * name)
{
mat_t* mf = reinterpret_cast<mat_t*>(pMF);
matvar_t* content = Mat_VarRead(mf, name);
if(content == 0)
{
return 0;
}
//Mat_VarPrint(content, 1);
return reinterpret_cast<mxArray*>(content);
}
/// fake libmx funcs
int mxGetM(const mxArray* arr)
{
const matvar_t* content = reinterpret_cast<const matvar_t*>(arr);
return content->rank >= 1 ? content->dims[0] : 0;
}
int mxGetN(const mxArray* arr)
{
const matvar_t* content = reinterpret_cast<const matvar_t*>(arr);
if(content->rank < 2)
{
return 0;
}
int ret = 1;
for(int i = 1; i < content-> rank; ++i)
{
ret *= content->dims[i];
}
return ret;
}
mxArray* mxGetCell(const mxArray* arr, int idx)
{
const matvar_t* content = reinterpret_cast<const matvar_t*>(arr);
matvar_t* cell = Mat_VarGetCell(const_cast<matvar_t*>(content), idx);
return reinterpret_cast<mxArray*>(cell);
}
mxArray* mxGetField(const mxArray* arr, int idx, const char* name)
{
const matvar_t* content = reinterpret_cast<const matvar_t*>(arr);
matvar_t* field = Mat_VarGetStructFieldByName(const_cast<matvar_t*>(content), name, idx);
return reinterpret_cast<mxArray*>(field);
}
void* mxGetData(const mxArray* arr)
{
// Just works on this single line!!
return reinterpret_cast<const matvar_t*>(arr)->data;
}
void mxDestroyArray(mxArray* arr)
{
Mat_VarFree(
reinterpret_cast<matvar_t*>(arr)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment