Generate mex file to call "Load(fileAddress)" function
1 view (last 30 days)
Show older comments
Hi all,
I'm dealing with large .mat files (multiple files and each ~600 MB). Reading the files is a time-consuming process and I was wondering employing a mex function to facilitate that.
How can I have a mex function to load *.mat files? I've tried generating the function using the coder but it says that file address should be a constant and fails generating the mex.
I would like to have a function call such as:
content = loadMex(fileAddress);
Thanks,
1 Comment
James Tursa
on 4 Mar 2024
The mex function would just call MATLAB API routines to read the mat file. Why do you think this will be faster than loading the mat file from your m-code?
Answers (1)
Nathan S
on 5 Mar 2024
There are a two main options:
- You can read the mat file extrinsically. This would mean that your generated code just calls back to MATLAB, and reads the mat file data there, then passes the information back into the mex.This doc page explains how you can do this - make sure to definie a proper type for the results of your load if you want to use the data in your generated code. If you are not using the data in the mat file in your geneated code, this will not make anything faster (all of the passing back and forth will probably make it slower).
- You can use something that isn't a mat file. coder.read uses a special data format designed for codegen, or you can use a data file of your own design with fread or fscanf. The various options and their advantages/disadvantages are listed here.
It will never be possible to have a function with exactly the API that you gave here. MATLAB Coder needs to assign a type to all variables to match the behavior of C/C++. In order to know the type of the data in the mat file, we either need to read it (which requires a constant mat file name), or you need to tell us in some way (which your example doesn't include)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!