Saving data from C to a MATLAB data file
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
.matfile -
The prototype for the open function is
MATFILE *openmatfile(char *fname, int *err);where
fnameis the filename anderrreceives 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
mfis the MATLAB file pointer from the open statement;nameis acharstring containing the name that the matrix will be given in MATLAB;datais a C data array of typedouble;mandnare the array dimensions; andtransposetakes the value of0or1to indicate where the matrix is to be transposed. The returned integer is zero0if 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
mfis the MATLAB file pointer from the open statement;nameis acharstring containing the name that the matrix will be given in MATLAB; andstris the string. The returned integer is zero0if successful.For example, to add a
string, the call might bematfile_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
mfis the MATLAB file pointer from the open statement and the returned integer is zero0if 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:- In Eclipse’s left pane of the
perspective, select
172.22.11.2, and press F5 to refresh the files. - Expand
172.22.11.2to show 172.22.11.2/Sftp Files/My Home/Lab4.mat. Select the fileLab4.matand copy it with Ctrl + c. - In the same pane of the Remote System Explorer, expand
Localto show Local Files/Drives. Paste the data file by selectingZ:and pressing Ctrl + v. This drive is special because it is shared between the guest and host operating systems. - On the host, navigate to the folder where you ran
vagrant upin section A.4. This folder should have the same contents as theZ:drive in the VM. Now double-click thelab4.matfile to open it in MATLAB. Use MATLAB’swhos()command to list all the named variables in the workspace. The file can later be opened from a MATLAB script using the commandload('Lab4.mat'), for plotting or analysis.
If you double-click the
Lab4.matfile in the perspective, it will also appear in the RemoteSystemsTempFiles directory within yourworkspace. - In Eclipse’s left pane of the
perspective, select
Online Resources for Section D.1
No online resources.