Export to Matlab

Export to Matlab

A lot of my team still uses Matlab for data analysis and processing. I haven't converted them all over to FAMOS, so until then, I still need to be able to export to Matlab, or .mat format.

I know how to do it manually (i.e. select the variables you want, save together, select .mat format).

What I am interested in is code I can put into a sequence that will perform the same function. I tried using

save _Test "_Test.mat"

This did create a Matlab file, _Test.mat. I tried to import it back into FAMOS and it gave me errors. I also noticed the file size was off (usually converting to Matlab is around an 8x-10x file size increase from the .dat file).

Any suggestions?

Re: Export to Matlab

Well, the size was off because the "save" command will only save in the imc FAMOS (DSF) format -- in other words, your _Test.mat file was really _Test.dat, but with the wrong extension.

Remember, in Window, the file extension is more a "reminder" or a "suggestion" of how to interpret the file; Windows does absolutely nothing to enforce this. It is up to us!

Exporting data using a compliled export definition (like the Matlab Format DLL included with FAMOS 5 and 6) is exactly the same as saving data in imc format, with the exception that you must specify which [i]template[/i] to use when you first open the output file:

FileID = FileOpenFAS("_test.mat", "#matlab_exp", 101) ; Or use FileOpenDSF for FAMOS format
FileErr = FileObjWrite(FileID, _test)
FileErr = FileClose(FileID)

Where _test.mat is your output file, and the export definition (e.g. matlab_exp.dll) is the DLL on your system.

[i]Note: The Matlab export has evolved a bit over the years, so you may want to look in C:\Program Files\imc\Shared\Extensions\ to see what "matlab-like" files you have there. FAMOS 5 supported a Matlab4 format, while FAMOS 6 supports a Matlab5 format as well.[/i]

By the way, the file is actually "written" on the FileClose; you can think of the FileObjWrite command(s) as simply queueing the data to be written.

Re: Export to Matlab

Oh, and if you have more than one channel to output into this file, you would use a separate "FileObjWrite" for each.

This can get tedious, though, if you have a lot of channels, or you aren't sure of the names of the channels when you write the sequence.

One trick I like to use it to put all my output data into a group. Let's say you called the group _test (continuing from your previous question). Now, I don't care how many channels are in _test, or what they are called; I can handle it in a completly generic fashion:

FileID = FileOpenFAS("_test.mat", "#matlab_exp", 101) ; Or use FileOpenDSF for FAMOS format

_i = 1
while _i <= GrChanNum?(_test)
FileName = GrChanName?(_test, _i)
FileErr = FileObjWrite(FileID, _test:<FileName>)
_i = _i + 1

FileErr = FileClose(FileID)

I like this because I usually put all my "results" into a group (called R or Results), and it is usually the results that I am interested in outputting. The "101" option removes the group name from the output file variable names, so it doesn't mess you up later.

Similar "looping over channels" can be done with the powerful VarGet* commands to create a list of channels, too.


  • 1