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
.mat
file -
The prototype for the open function is
*openmatfile(char *fname, int *err); MATFILE
where
fname
is the filename anderr
receives any error code. The function returns a structure for containing the MATLAB file pointer.A typical call might be
= openmatfile("Lab4.mat", &err); mf 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( *mf, MATFILE char *name, double *data, int m, int n, int transpose );
where
mf
is the MATLAB file pointer from the open statement;name
is achar
string containing the name that the matrix will be given in MATLAB;data
is a C data array of typedouble
;m
andn
are the array dimensions; andtranspose
takes the value of0
or1
to indicate where the matrix is to be transposed. The returned integer is zero0
if successful.For example, to add a one-dimensional matrix, the call might be
(mf, "vel", buffer, IMAX, 1, 0); matfile_addmatrix
Or, to add a single variable, the call might be
double Npar; = (double) N; Npar (mf, "N", &Npar, 1, 1, 0); matfile_addmatrix
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 achar
string containing the name that the matrix will be given in MATLAB; andstr
is the string. The returned integer is zero0
if successful.For example, to add a
string
, the call might be(mf, "myName", "Bob Smith"); matfile_addstring
- 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 zero0
if successful.For example, to close the MATLAB file, the call might be
(mf); matfile_close
- Example code
-
Putting these ideas together, we get:
= openmatfile("Lab4.mat", &err); mf if(!mf) printf("Can't open mat file %d\n", err); (mf, "myName", "Bob Smith"); matfile_addstring(mf, "N", &Npar, 1, 1, 0); matfile_addmatrix(mf, "M", &Mpar, 1, 1, 0); matfile_addmatrix(mf, "vel", buffer, IMAX, 1, 0); matfile_addmatrix(mf); matfile_close
- 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
172.22.11.2
, and press F5 to refresh the files.
perspective, select - Expand
172.22.11.2
to show 172.22.11.2/Sftp Files/My Home/Lab4.mat. Select the fileLab4.mat
and copy it with Ctrl + c. - In the same pane of the Remote System Explorer, expand
Local
to 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 up
in section A.4. This folder should have the same contents as theZ:
drive in the VM. Now double-click thelab4.mat
file 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.mat
file in the perspective, it will also appear in the RemoteSystemsTempFiles directory within yourworkspace
. - In Eclipse’s left pane of the
Online Resources for Section D.1
No online resources.