dominicw
Forum-Anfänger
Beiträge: 15
Anmeldedatum: 13.06.08
Wohnort: ---
Version: ---
Verfasst am : 06.06.2009, 23:44
Titel : Probleme mit Mex File
Hallo,
ich muss binäre Dateien einlesen, die sehr groß sind. Wobei einzelne Datensätze nur 32 Byte haben. Das läuft auf eine riesige Schleife hinaus für die Matlab ewig braucht. Aus diesem Grund habe ich versucht diese Schleife in C zu übersetzen und ein Mex file zu erstellen.
Meine Matlab Funktion öffnet die Datei und liest den Header. Danach rufe ich über
[ArrayOfStructs] = function(fidIn,.....)
Mein Mex file auf. Leider schmiert mir dann Matlab ab. Ich vermute ich mache etwas mit der Speicherreservierung falsch.
Ich poste hier den kompletten C Code, aber ich denke interessant ist vor Allem wie ich vorhabe Speicher zu reservieren:
Hier möchte ich eigentlich Speicherplatz für ein temoräres Array aus Strukturen erzeugen. Es soll ein 1D Array mit Records/500 Einträge sein.
Das soll mein array of structures sein, das ich zurückgebe.
Code:
#include "mex.h "
#include "matrix.h "
#include <stdio.h >
#define T3WRAPAROUND 65536
#pragma pack ( 4 ) //structure alignment to 4 byte boundaries
/*The following data records appear for each T3 mode event*/
union {
unsigned int allbits;
struct
{
unsigned numsync :16 ;
unsigned dtime :12 ;
unsigned channel :4 ;
} bits;
struct
{
unsigned numsync :16 ;
unsigned markers :12 ;
unsigned channel :4 ;
} special;
} Record;
typedef struct structData {
int chan;
double dtime;
double truetime;
} Data;
/* the gataway function */
void mexFunction( int nlhs, mxArray *plhs[ ] ,
int nrhs, const mxArray *prhs[ ] )
{
int *Records;
int *maxNoOfPhotons;
double *syncperiod;
double *Resolution;
FILE *fidIn;
int i, index, index1, index2;
int result;
__int64 ofltime=0 ;
int line = 0 ;
int frame = 0 ;
int currentPixel;
bool flagFirstFrame = false ;
double lineStart = 0 ;
double lineEnd;
double t_pixel;
double t_line;
int x;
int imres = 512 ;
double macroTime;
double microTime;
double truensync;
double truetime;
mwSize dims[ 2 ] = { 1 , *maxNoOfPhotons } ;
const char *fieldNames [ ] = { "chan","dtime","truetime","x","line","frame"} ;
// preallocate lineData array of structures
Data *lineData;
lineData = mxMalloc( *Records/500 *sizeof( *lineData) ) ;
// passed parameters from matlab
fidIn = ( FILE*) mxGetPr( prhs[ 0 ] ) ;
Records = ( int *) mxGetPr( prhs[ 1 ] ) ;
syncperiod = ( double *) mxGetPr( prhs[ 2 ] ) ;
maxNoOfPhotons = ( int*) mxGetPr( prhs[ 3 ] ) ;
Resolution = ( double *) mxGetPr( prhs[ 4 ] ) ;
// creating output array of structures
plhs[ 0 ] = mxCreateStructArray( 2 , dims, 6 , fieldNames ) ;
// here begins the real function
for ( i=0 ;i<*Records;i++)
{
result = fread ( &Record, 1 , sizeof( Record) ,fidIn) ;
if ( result!= sizeof( Record) )
{
mexPrintf( "\nUnexpected end of input file!") ;
break ;
}
if ( Record.bits .channel ==0xF) //this means we have a special record
{
truensync = ( ( double ) ofltime+( double ) Record.special .numsync ) ;
truetime = truensync*( *syncperiod) ;
switch ( Record.special .markers )
{
case 0 : // overflow
ofltime += T3WRAPAROUND; // unwrap the time tag overflow
break ;
case 1 : // line start
line += 1 ;
index = 0 ;
lineStart = truetime;
break ;
case 2 : // line stop
if ( flagFirstFrame == true )
{
// sort lineData in the right pixel and add to Photon struct
lineEnd = truetime;
t_line = lineEnd - lineStart;
t_pixel = t_line / imres;
currentPixel=0 ;
for ( index1=0 ;index1<index;index1++ )
{
macroTime = lineData[ index1] .truetime ;
macroTime = macroTime - lineStart;
for ( index2=currentPixel;index2<imres;index2++ )
{
if ( macroTime > ( index2) *t_pixel && macroTime <= ( index2+1 ) *t_pixel )
{
x = index2+1 ;
currentPixel = index2;
break ;
}
}
mxSetFieldByNumber( plhs[ 0 ] ,index1,0 ,mxCreateDoubleScalar( lineData[ index1] .chan ) ) ;
mxSetFieldByNumber( plhs[ 0 ] ,index1,1 ,mxCreateDoubleScalar( lineData[ index1] .dtime ) ) ;
mxSetFieldByNumber( plhs[ 0 ] ,index1,2 ,mxCreateDoubleScalar( lineData[ index1] .truetime ) ) ;
mxSetFieldByNumber( plhs[ 0 ] ,index1,3 ,mxCreateDoubleScalar( x) ) ;
mxSetFieldByNumber( plhs[ 0 ] ,index1,4 ,mxCreateDoubleScalar( line ) ) ;
mxSetFieldByNumber( plhs[ 0 ] ,index1,5 ,mxCreateDoubleScalar( frame) ) ;
} // for ( index1=0 ;index1<index;index1++ )
} // if ( flagFirstFrame == true )
break ;
case 4 : // new frame
if ( flagFirstFrame == false ) ;
{
flagFirstFrame = 1 ;
frame = 0 ;
}
frame += 1 ;
line = 0 ;
break ;
case 6 : // line stop + new frame
if ( flagFirstFrame == true )
{
// sort lineData in the right pixel and add to Photon struct
lineEnd = truetime;
t_line = lineEnd - lineStart;
t_pixel = t_line / imres;
currentPixel=0 ;
for ( index1=0 ;index1<index;index1++ )
{
macroTime = lineData[ index1] .truetime ;
macroTime = macroTime - lineStart;
for ( index2=currentPixel;index2<imres;index2++ )
{
if ( macroTime > ( index2) *t_pixel && macroTime <= ( index2+1 ) *t_pixel )
{
x = index2+1 ;
currentPixel = index2;
break ;
}
}
mxSetFieldByNumber( plhs[ 0 ] ,index1,0 ,mxCreateDoubleScalar( lineData[ index1] .chan ) ) ;
mxSetFieldByNumber( plhs[ 0 ] ,index1,1 ,mxCreateDoubleScalar( lineData[ index1] .dtime ) ) ;
mxSetFieldByNumber( plhs[ 0 ] ,index1,2 ,mxCreateDoubleScalar( lineData[ index1] .truetime ) ) ;
mxSetFieldByNumber( plhs[ 0 ] ,index1,3 ,mxCreateDoubleScalar( x) ) ;
mxSetFieldByNumber( plhs[ 0 ] ,index1,4 ,mxCreateDoubleScalar( line ) ) ;
mxSetFieldByNumber( plhs[ 0 ] ,index1,5 ,mxCreateDoubleScalar( frame) ) ;
} // for ( index1=0 ;index1<index;index1++ )
} // if ( flagFirstFrame == true )
flagFirstFrame = true ;
frame += 1 ;
line = 0 ;
break ;
} // switch
} // if ( Record.bits .channel ==0xF)
else
{
truensync = ( ( double ) ofltime+( double ) Record.bits .numsync ) ;
truetime = truensync*( *syncperiod) + ( double ) Record.bits .dtime* ( *Resolution) ;
lineData[ index] .chan = Record.bits .channel ;
lineData[ index] .dtime = Record.bits .numsync ;
lineData[ index] .truetime = truetime;
}
} //for ( i=0 ;i<*Records;i++)
mxFree( lineData) ;
} //mexFunction
Wäre schön, wenn es sich jemand mal ansehen könnte. Auch wenn es im ersten Moment sicher viel und verwirrend wirkt.
Vielen Dank
dominicw
Themenstarter
Forum-Anfänger
Beiträge: 15
Anmeldedatum: 13.06.08
Wohnort: ---
Version: ---
Verfasst am : 07.06.2009, 10:17
Titel :
Ok, ich kann den fid nicht an C übergeben. Ich schließe jetzt die Datein Matlab und öffne sie erneut im Mex File.
dominicw
Themenstarter
Forum-Anfänger
Beiträge: 15
Anmeldedatum: 13.06.08
Wohnort: ---
Version: ---
Verfasst am : 07.06.2009, 12:47
Titel :
Ich habe jetzt herausgefunen wie man ein mex file debuggt und weiß nun das es an folgender Stelle zum Absturz kommt
Einstellungen und Berechtigungen
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
Impressum
| Nutzungsbedingungen
| Datenschutz
| FAQ
| RSS
Hosted by:
Copyright © 2007 - 2025
goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.