How do I write an MDF4 file from a script?

The MDF4 add-in of UniPlot can write MDF4 files as well as read them. In UniScript you describe the channel groups and channels in a nested object and pass it to mdf4_create(). This is useful to hand simulation results or calculated data to tools that expect MDF4, or to create test files.

Example

One channel group with a time channel and two signals:

o = [. n = 1,
    1 = [. n = 3,
        1 = [. name = "time", data = [0.1, 0.2, 0.3]],
        2 = [. name = "sig1", data = [3, 1, 7]],
        3 = [. name = "sig2", data = [1, 1, 1]]]];
mdf4_create(o, "d:/test.mf4");

o.n is the number of channel groups. Each group has its own n (number of channels) and one element per channel with name and data.

How it works

  1. Load the MDF4 Import add-in once with Tools=>Add-In Manager and restart UniPlot.

  2. Build the object: one numbered element per channel group, inside it one numbered element per channel.

  3. Call mdf4_create(o, ssFileName).

  4. Check the result by importing the file with File=>Import Data (file type MDF4 File (*.mf4)) or read it back in the script.

Reading MDF4 files in a script

To read an MDF4 file channel by channel without converting it, use the reading functions:

oMDF = mdf4_open("d:/test.mf4");
oGroups = mdf4_open_groups(oMDF);
oGroup = mdf4_open_group(oGroups, 1);
oChannels = mdf4_open_channels(oGroup);
oChannel = mdf4_open_channel(oChannels, 2);
rvData = mdf4_channel_get_data(oChannel);
mdf4_close(oMDF);

mdf4_groups_count and mdf4_channels_count return the number of groups and channels.

Why it helps

  • MDF4 output directly from UniScript, without a third-party library.

  • Writing and reading use the same add-in, so a script can create a file and check it right away.

  • Data calculated in UniPlot can be passed on to tools that only accept MDF4.

Good to know

  • The source code is included: mdf4_read.ic, mdf4_write.ic and rs_mdf4.ic in the directory addin\rs_mdf4.

  • To convert an MDF4 file into UniPlot’s own NC2 format, use the normal import (auto_ImportData) instead, see How do I open and analyze MDF4 files?.