-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat2cpp.m
36 lines (32 loc) · 858 Bytes
/
mat2cpp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
% MAT2CPP - Write matrix
% mat2cpp(A) writes MATLAB matrix A to C++ (for use in .cpp)
%
% INPUT:
% A = m x n matrix
% matname = Name of matrix (default 'A')
%
%
% See also fopen, fclose, matpak
% -------------------------------------------------------------------------
% Quinlan, J.
% 2023-02-19
% -------------------------------------------------------------------------
function mat2cpp(A,matname)
if nargin < 2
matname = 'A';
end
matname = strcat(matname);
[m,n] = size(A);
% Write to File
fid = fopen(strcat(matname,'.txt'),'w');
fprintf(fid,'%s = {\n',matname);
for i = 1:m
fprintf(fid,'{%e, ',A(i,1));
for j = 2:n-1
fprintf(fid,'%e, ',A(i,j));
end
fprintf(fid,'%e },\n ',A(i,n));
end
fprintf(fid,'};');
fclose(fid);
end