RTC Website

Saving data from C to a MATLAB data file

DX

The following C functions write data of double or char types to a MATLAB .mat data file. They rely on a software package by mclean2022 and are included in the T1 C library. Be sure to #include "matlabfiles.h".

Use the following functions to open a named file on the myRIO target computer and successively add any number of data arrays, variables, and strings to the file. Finally, close the file.

Open a .mat file

The prototype for the open function is

MATFILE *openmatfile(char *fname, int *err);

where fname is the filename and err receives any error code. The function returns a structure for containing the MATLAB file pointer.

A typical call might be

mf = openmatfile("Lab4.mat", &err);
if(!mf) printf("Can't open mat file %d\n", err);

Notice the use of pointers.

Add a matrix

The prototype of the function for adding a matrix to the MATLAB file is

int matfile_addmatrix(
  MATFILE *mf,
  char *name,
  double *data,
  int m,
  int n,
  int transpose
);

where mf is the MATLAB file pointer from the open statement; name is a char string containing the name that the matrix will be given in MATLAB; data is a C data array of type double; m and n are the array dimensions; and transpose takes the value of 0 or 1 to indicate where the matrix is to be transposed. The returned integer is zero 0 if successful.

For example, to add a one-dimensional matrix, the call might be

matfile_addmatrix(mf, "vel", buffer, IMAX, 1, 0);

Or, to add a single variable, the call might be

double Npar;
Npar = (double) N;
matfile_addmatrix(mf, "N", &Npar, 1, 1, 0);

Again, note the use of pointers and the cast to double.

Add a string

The prototype of the function for adding a string to the MATLAB file is

int matfile_addstring(MATFILE *mf,
                      char *name,
                      char *str);

where mf is the MATLAB file pointer from the open statement; name is a char string containing the name that the matrix will be given in MATLAB; and str is the string. The returned integer is zero 0 if successful.

For example, to add a string, the call might be

matfile_addstring(mf, "myName", "Bob Smith");
Close the file

After all data have been added, the file must be closed. The prototype of the function for closing the MATLAB file is

int matfile_close(MATFILE *mf);

where mf is the MATLAB file pointer from the open statement and the returned integer is zero 0 if successful.

For example, to close the MATLAB file, the call might be

matfile_close(mf);
Example code

Putting these ideas together, we get:

mf = openmatfile("Lab4.mat", &err);
if(!mf) printf("Can't open mat file %d\n", err);
matfile_addstring(mf, "myName", "Bob Smith");
matfile_addmatrix(mf, "N", &Npar, 1, 1, 0);
matfile_addmatrix(mf, "M", &Mpar, 1, 1, 0);
matfile_addmatrix(mf, "vel", buffer, IMAX, 1, 0);
matfile_close(mf);
Transfer the file to MATLAB

After the file (say, Lab4.mat) has been created, it can be transferred directly to MATLAB by performing the following steps:

  1. In Eclipse’s left pane of the Remote Systems Explorer perspective, select 172.22.11.2, and press F5 to refresh the files.
  2. Expand 172.22.11.2 to show 172.22.11.2/Sftp Files/My Home/Lab4.mat. Select the file Lab4.mat and copy it with Ctrl + c.
  3. In the same pane of the Remote System Explorer, expand Local to show Local Files/Drives. Paste the data file by selecting Z: and pressing Ctrl + v. This drive is special because it is shared between the guest and host operating systems.
  4. On the host, navigate to the folder where you ran vagrant up in section A.4. This folder should have the same contents as the Z: drive in the VM. Now double-click the lab4.mat file to open it in MATLAB. Use MATLAB’s whos() command to list all the named variables in the workspace. The file can later be opened from a MATLAB script using the command load('Lab4.mat'), for plotting or analysis.

If you double-click the Lab4.mat file in the Remote Systems Explorer perspective, it will also appear in the RemoteSystemsTempFiles directory within your workspace.

Online Resources for Section D.1

No online resources.