f



How to call or pass MATLAB file pointer in C Mex file?

Hello all,

1. I am trying to read large binary data files (~1 GB size), process the information and dump the outputs (which are also of incredibly big size) to MATLAB workspace. I wrote a mex file to do the same (so that I have faster program execution speeds).

2. The C mex file that I wrote works very well and can dump all the variables to MATLAB workspace for smaller size files. However for larger binary files, the MATLAB gives OUT OF MEMORY error as the size of the variable space is simply too big.

3. Often I am not interested in all portions of the file and don't need all the variables in my workspace. So I thought of reading only as much portion of these files as can be supported by the memory available to MATLAB. 

4. Currently, the file name and path is passed from MATLAB to mex function and the file is opened, read and closed in the mex function itself. If there is a way to call or pass file pointer from MATLAB to mex function, I can open the file in MATLAB (using fopen), pass the pointer to mex function, get the outputs for some fixed small portion of the file, call the mex function again (so that it reads the file starting from where it left it in last call) and so on and so forth till I get the data I actually need. So, in this way, I can call the mex function again and again and check what portion I actually need and then proceed with the rest of my program.

5. However the problem is I am not aware if there exists a way to call or pass the MATLAB fopen file pointers to C mex file?  Please help.

Any help will be greatly appreciated.

Anindya G.
0
12/13/2008 7:47:06 AM
comp.soft-sys.matlab 211266 articles. 24 followers. lunamoonmoon (257) is leader. Post Follow

4 Replies
929 Views

Similar Articles

[PageSpeed] 50

"Anindya G" <kamaskar1980@gmail.com> wrote in message <ghvp9q$obf$1@fred.mathworks.com>...
> 
> 4. Currently, the file name and path is passed from MATLAB to mex function and the file is opened, read and closed in the mex function itself. If there is a way to call or pass file pointer from MATLAB to mex function, I can open the file in MATLAB (using fopen), pass the pointer to mex function, get the outputs for some fixed small portion of the file, call the mex function again (so that it reads the file starting from where it left it in last call) and so on and so forth till I get the data I actually need. So, in this way, I can call the mex function again and again and check what portion I actually need and then proceed with the rest of my program.
> 
> 5. However the problem is I am not aware if there exists a way to call or pass the MATLAB fopen file pointers to C mex file?  Please help.
> 

No. You can't do this. But you can accomplish the same thing from within your one mex file. Just have the FILE pointer at the top level so that it retains its value from call to call, and don't close the file in the mexFunction routine between calls. Set up a mexAtExit function to close the file. If you want to close the file prematurely, just clear the mex function. e.g., here is an outline:

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

FILE *fp = NULL;
// have other top level variables here also if you need to

void ExitFcn(void)
{
    if( fp != NULL ) {
        fclose(fp);
        fp = NULL;
    }
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    if( fp == NULL ) {
        fp = fopen(  etc  ); // open the file here
        if( fp == NULL ) {
            mexErrMsgTxt("Unable to open file");
        }
        mexAtExit( ExitFcn );
    }
    // process a chunk of the file here, starting where you left off
    // if you hit the EOF, then call ExitFcn here
}

James Tursa

0
12/13/2008 9:23:03 AM
On 13 Des, 08:47, "Anindya G" <kamaskar1...@gmail.com> wrote:

> 4. Currently, the file name and path is passed from MATLAB to mex functio=
n and the file is opened, read and closed in the mex function itself. If th=
ere is a way to call or pass file pointer from MATLAB to mex function, I ca=
n open the file in MATLAB (using fopen), pass the pointer to mex function, =
get the outputs for some fixed small portion of the file, call the mex func=
tion again (so that it reads the file starting from where it left it in las=
t call) and so on and so forth till I get the data I actually need. So, in =
this way, I can call the mex function again and again and check what portio=
n I actually need and then proceed with the rest of my program.

The safe way is to open and close the file in the mex routine,
return the file pointers from the C routine back to matlab,
and use this file pointer as argument when you call the file
access routine the next time. Check out the functions fseek
and ftell in the <stdio.h> library.

This will cost a little bit of time, as you need to open
and close the file each time you access the C routine, but
you avoid the intricacies of coordinating static variables
between C and matlab.

Rune
0
allnor (8509)
12/13/2008 10:24:30 AM
Thanks James and Rune for your replies. I had tried the method suggested by Rune before but didn't want to use it precisely because of the reason that it was slower. I tried the way suggested by James (i.e. keeping my File pointer global and using ExitFcn) and it works very well.

Thanks again both of you.

Sincerely,

Anindya G.
0
12/13/2008 8:04:02 PM
> 3. Often I am not interested in all portions of the file and don't need  
> all the variables in my workspace. So I thought of reading only as much  
> portion of these files as can be supported by the memory available to  
> MATLAB.
You could try memory-mapping the file:

Please find more details online at:
     http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/braidws-1.html#braidws-35
Or please paste this in the MATLAB command window:
     web([docroot,'/techdoc/matlab_prog/braidws-1.html#braidws-35'])
0
12/13/2008 10:44:10 PM
Reply:

Similar Artilces:

how to call matlab file to another matlab file
Hey friends, i am working on simulation of power system using matlab. for that i have written codes for generation side and line side separately. Now i want to use the result of generation file to line file....so how can i use the result of one file to another.... eg i have gen.m file from that i can get hourly gen data....now i want to compare that data to line.....and line modelling is done in separate file line.m so i want to call result of gen.m file to line.m file..... waiting for ur valuable suggestion thanks ...

methods to call matlab in C C++ and call C C++ in matlab
Hi, I am reading the help documents about how to program matlab and C C++ together and my questions are a little bit general. It seems that are several ways to call matlab in C C++ and another several ways to call C C++ in matlab. Please correct me if I am wrong or missing something when I try to list them. To call matlab code in C C++, are there two methods available? The first one is Matlab engine and the second one is to use MATLAB Compiler mcc to create C or C++ shared libraries from your MATLAB code. To call C C++ in matlab, are there also two methods? The first one is to call funct...

pass 3D Matrix from matlab to C mex- file
help please how can i pass 3d matrix from matlab to c using mex file thanks baavour@gmail.com wrote: > help please > > how can i pass 3d matrix from matlab to c > > using mex file This is a mailing list for the Python programming language. Please ask your question on the appropriate Matlab mailing list. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco ...

How to pass complex structure from matlab to C++ mex file?
Hello, I want to pass a Matlab structure (called 'para') to a C++ mex file 'gateway.mex'. The fields of the structure can be complex, numeric or a string. The data type of the field are decribed below: para.fieldname1 is a numeric para.fieldname1 is a string para.fieldname1 is a complex number In the mex file (called by gateway(para)) I write something like this to get the values of the different fields. mxArray *tmp; double dbl,*pre,*pim; char *str; To get the numeric field : tmp = mxGetField(prhs[0],0,fieldname1); dbl=mxGetScalar(tmp); To get the string field: tmp = m...

Pass a 3d matrix from matlab to C mex file
please i need help how can i pass a 3d matrix from matlab to C mex file thanks Its the same as passing in a 2D matrix. You can get individual elements like: void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double * pData; mwSize * pSize; mwSize nDims; double DesElement; nDims = mxGetNumberOfDimensions(prhs[0]); pSize = mxGetDimensions(prhs[0]); pData = mxGetPr(prhs[0]); //Then to get element at equivilent Matlab index (r,c,d) DesElement = pData[r-1 + (c-1)*pSize[0] + (d-1)*pSize[0] *pSize[1]]; } "Bavi b...

Segmentation violation after calling mex c file in matlab
Hi everyone, i'm new in calling C file whitin matlab. I wrote a c function called intex : double* intex(double *zrd_a, int dim_of_matrix, double delta_t, int fa, int is_phi_h); and its mexFunction in C. As you can see, i read the matrix "zrd_a" to calculate and give as output another matrix. The problem is after calling the C method a certain time (5 or 10 times), Matlab craches and show the following error: ---------------------------------------------------------------------- -- Segmentation violation detected at Tue Dec 12 12:15:55 2006 -----------------------------...

[C file I/O (via mex)] content written to file is only present if matlab is closed
Hello, I have c-function interfaced via mex. Inside this function several file streams are opened, content is written using fprintf() and the files are closed. Some file descriptors are defined within the function scope, opened there, written there and closed there. With these files all is fine. But one file descriptor is declared globally, opened in one function, written to in another and finally closed in the function, where it has been opened. The problem with this file is, that after a successful call of the mex function, the file size is 0 bytes. The file is opened with "w" ...

Problem creating Mex file when calling Matlab from C++; DLL
Hi, I have some MS Visual C++ 2008 code that is compiled using mex command into dlls; and the code is executed from Matlab 2010a. I now want to call some Matlab commands from within the C++ functions (to solve large complex linear equations/matrix inversion). From what I could read online and on the Matlab engine webpage, I will have to create an exe file rather than dll to call Matlab from C. I'm not familiar with either dlls or exes, and I am unable to compile the code correctly now. I get the error: LINK : fatal error LNK1561: entry point must be defined I will apreciate if s...

Pass a 3d matrix from matlab to C mex file #2
please i need help how can i pass a 3d matrix from matlab to C mex file thanks ...

I have a m-file that I compiled into a dll using MatLab compiler. I then created a C# library class to call the MatLab dll
hi, >i creat an M-file which has an imagen in file then in matlab i create it dll using >deploytool >Then i include this dll in visual studio 2005 C#.Net project >Then i make a windows form which has a button,picturebox,textbox openfiledialog >what should be the code of button it access the m-file from matlab show its path in >textbox and image in picture box or >what is the procedure of accesing matlab file from C#.Net,plz help me for this >problem immediatley >my code of m-file is function aa %Ori_Face=imread('E:\Shakir\Faisal\Face\sub6.1.jpg'); ...

load5
As a solution to some posts a while ago about reading compressed matlab 7 files in matlab 6, i posted load5 to the file exchange (7734) which does this. Note that the "Other Requirements" field on the FX is outdated as the zip file has both windows and linux versions and DOES search the matlab path if the file is not found in the current directory. ...

C++ Mex file crashes matlab on 64bit linux, but not 32 bit windows, but program runs fine outside Matlab
Hello, I have written a mex gateway function to a C++ class. If I compile this mex function on 32Bit windows using R2008a I have no problems. If I compile and run on Matlab R2011a running 64bit Scientific Linux (a version of Red Hat Enterprize Linux) matlab exits with a segfault when the mexfunction is called, although it appears to run about halfway through the program. The C++ class can be compiled and run (with a main function) outside of Matlab on both platforms with no errors. I am using Microsoft Visual C++ Express Edition on the windows machine, and gcc 4.4.5 on the Linux machine. ...

How do I call a subfunction fom a ".m" file in my matlab dir. with mexCallMatlab, C++ MEX
I am a beginner at MEX functions, and i have a big main program with a funtion like this: function c=speedOfSound global pSettings; c=pSettings.c; I want to call this variable from my MEX function callded from the main ".m" file, and have tried to do this in the following manners: 1: mxArray *properties[1]; mexCallMATLAB(1, properties, 0, NULL, "speedOfSound"); double c=*(mxGetPr(properties[0])); 2: mxArray * Speedofsound=mexGetVariable("global", "pSettings.c"); double c=*(mxGetPr(Speedofsound)); Both choices gives me a errors...

Is it possible to convert a matlab files into a .c files ?
hi i am fascinated about Is it possible to convert a matlab files into a .c files or python ? is there any easy way for doing this ? if yes please tell me how ??? "ofdma_matlab" wrote in message <j5sf1u$hnl$1@newscl01ah.mathworks.com>... > hi i am fascinated about Is it possible to convert a matlab files into a .c files or python ? > is there any easy way for doing this ? > if yes please tell me how ??? Hi, yes, see http://www.mathworks.com/products/matlab-coder/ to generate C or C++ code. Wayne ...

Web resources about - How to call or pass MATLAB file pointer in C Mex file? - comp.soft-sys.matlab

Plaxton Pointer - Wikipedia, the free encyclopedia
The Plaxton Pointer (originally known as the Reeve Burgess Pointer , and later as the TransBus Pointer and Alexander Dennis Pointer ) is a successful ...

Michael Pointer (@michaelpointer) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Thread: I'm looking for pointers to a Google Maps API overview.
... outline for a map, making it super-easy to edit, and also to have it in the most convenient form for someone browsing over the data. Any pointers ...

Anchor Pointer — GPS Compass & Friend Navigator on the App Store on iTunes
Get Anchor Pointer — GPS Compass & Friend Navigator on the App Store. See screenshots and ratings, and read customer reviews.

Kobe Bryant 45 Points (NBA Record 12 Three-Pointers) vs Seattle Super Sonics - Highlights 07/01/2003 ...
Los Angeles Lakers vs Seattle Super Sonics 07/01/2003 Ignore Tags : mix derrick rose mix miami heat new york knicks mix thunder lebron james ...

Shark warning as white pointers cruise off Perth beaches
Large schools of salmon are bringing danger to Perth's beaches and waters as Great White Sharks follow the fish along the coast.

Footy kicks logic for a nine-pointer
Apart from hilarious change strips, footy’s number one growth industry appears to be inexplicable phenomena

Juvenile white pointer shark nursery located off beaches around Newcastle
... nowhere to be seen. The stretch of coast just north of Newcastle has long had a reputation as being a “nursery” for juvenile white pointer sharks. ...

Esperance shark attack survivor Sean Pollard recalls how he fought off white pointer - PerthNow Search ...
BUNBURY shark attack survivor Sean Pollard says he feels lucky to be alive after his miracle escape from a white pointer near Esperance.

Georgia State Panthers coach Ron Hunter falls off stool as son RJ Hunter nails match-winning three-pointer ...
Georgia State coach Ron Hunter fell out of his chair. The rest of America probably did, too.

Resources last updated: 2/13/2016 8:33:09 PM