Showing results for 
Search instead for 
Did you mean: 

Linker Problems When Trying to Write to .MAT File Format

Hi everyone,


        I've been trying on and off for quite a while trying to figure out how to save to a *.mat file from within LabWindows CVI.  I am aware that this is not a feature that comes with the software.  The Mathworks, however, does provide an external interface library (see, for example: ).  This seems like it should be a great solution, with simple functions to take care of all of my needs.  There are header files and library files available, with library files for the LCC compiler, the Borland compiler, and Microsoft's compiler, for use in Visual Studio.  


       I've added both the header files and the library files to the project (I used the LCC library files because the CVI documentation said that the standard compiler is a customized version of the LCC compiler). I tried a short program that simply opened a new .mat file, wrote a small array, and then closed it.  The program compiles just file, but it will not link and run.  For each of the libraries, it says that it cannot read the file, and loading the library was aborted. I did not compile with any sort of special command line options or any special settings most of the time, although I've tried just about every combination of those things that I can think of.  I've even tried using the .lib files that are for Visual Studio or Borland, but with no success (different errors, though). 


       I really am at a loss for any ideas to try even at this point.  I just cannot for the life of me get this library to link within CVI.  I suppose that I could try to just move the entire project over to Visual Studio, but I'd really rather not.  There is an open source program to write to .mat as well (MATIO, on sourceforge), and I've compiled that for use with GCC (the default for the configure and make files).  I haven't built it for LCC yet or tried it, but I don't see why it should be any different. 


       I have made sure that the lib files are in a directory that the program has permission to read from, if that helps.  The code is unremarkable, and I think copied either from the MATLAB documentation or another online example.  It is basically:






#include "mat.h"

#include "matrix.h"


int main(void)


        MATFile * matFile;

        char filename[] = "test.mat";

        mxArray *X;

        mxArray *Y;

        matFile = NULL;

        matFile = matOpen(fileName,"w");





I have tried it with and without #pragma comments at the top (such as #pragma comment(lib, "libmat.lib"), which I saw on an example program).


The link errors that I get are: 

4 Project link errors:

Read error.

Aborted load of library "c:\Program Files\MATLAB\R2011a\extern\lib\win32\lcc\libmat.lib"

Read error.

Aborted load of library "c:\Program Files\MATLAB\R2011a\extern\lib\win32\lcc\libmex.lib"


Any help would be very much appreciated.  I have a feeling this would be a simple problem for someone who does this kind of stuff frequently to solve, but I don't have any formal training in this sort of thing (I'm a mechanical engineer), and it's been extroardinarily frustrating for me.



0 Kudos
Message 1 of 6

Hi rbond0087,


I don’t have MatLab, but maybe I can still help. Can you use the MathWorks interface to write the .mat file? ( I know it’s a different approach then you’ve been trying, but perhaps you can achieve the same functionality.  




Michael Miracle

NI Americas | AE

0 Kudos
Message 2 of 6

I have build a different matlab-CVI tool with the libraries provided by matlab in ../extern/lib/win32/microsoft.

These are libmat.lib, libmex.lib and libmx.lib. I probably tried several until I got no errors anymore.

Additional I had at the top of my code a pragma for packing. I probably found a hint in one of the matlab examples:

#pragma pack(8)
#include <mex.h>
#pragma pack()


Succes, Jos


Message 3 of 6

Thanks a lot for the reply.  When I try to use the #pragma pack pre-processor directives that you suggested with the microsoft lib files, the program does link successfully and run, but immediately crashes with a message indicating that it cannot load libut.dll.  I do have the matlab compiler runtime installed on my computer, and even when I explicitly try to include the libut.dll file in the build, it gives the same error.  Do you have any advice on that issue?


Thanks again,


0 Kudos
Message 4 of 6

Sorry but I have not experienced this myself. By sheer luck I found a few lib-files that I had to include in my apllication and libut was not one of them. My only suggestion: check if libut.dll (probably in ..../matlab//R####/bin/win##) is in the common search path of your computer for windows applications. Or make a copy of your own application in that matlab subdirectory and try to run it. If this works, you can ask one of the real software engineers (not me) how to solve such a path-problem.


Succes, Jos


0 Kudos
Message 5 of 6

If your purpose is to export data from a CVI application towards matlab, an easy alternative is to design your own binary format.

Step 1: design an easy (structure) fileheader. Dump your fileheader with (repeated) data (arrays) in a binary file.

Step 2: write a matlab function-script that recreates the fileheader and reads the file header. Using the content in the fileheader, read the data and copy this to a matlab variable. Optional make a matlab function-script that writes the same format binary file.

Step 3: distribute the m-file together with the data files.

I have done this a few times for third-party files. Most difficult is to find and read description of (third party) file format. However if you can design this format yourself, this will be straightforward. If your SOLE PURPOSE is to exchange CVI data with matlab, I expect this to be a better solution than writing mat-files.


Regards, Jos


0 Kudos
Message 6 of 6