1 function [A,rows,cols,entries,rep,field,symm] = mmread(filename)
3 %
function [A] = mmread(filename)
5 %
function [A,rows,cols,entries,rep,field,symm] = mmread(filename)
7 % Reads the contents of the Matrix Market file
'filename'
8 % into the matrix
'A'.
'A' will be either sparse or full,
9 % depending on the Matrix Market format indicated by
10 %
'coordinate' (coordinate sparse storage), or
11 %
'array' (dense array storage). The data will be duplicated
12 % as appropriate
if symmetry is indicated in the header.
14 % Optionally, size information about the matrix can be
15 % obtained by
using the
return values rows, cols, and
16 % entries, where entries is the number of nonzero entries
17 % in the
final matrix. Type information can also be retrieved
18 %
using the optional
return values rep (representation), field,
19 % and symm (symmetry).
22 mmfile = fopen(filename,
'r');
25 error(
'File not found');
28 header = fgets(mmfile);
33 % NOTE: If using a version of Matlab for which strtok is not
34 % defined, substitute 'gettok' for 'strtok' in the
35 % following lines, and download gettok.m from the
37 [head0,header] = strtok(header); % see note above
38 [head1,header] = strtok(header);
39 [rep,header] = strtok(header);
40 [field,header] = strtok(header);
41 [symm,header] = strtok(header);
46 if ( length(symm) == 0 )
47 disp(['Not enough words in header line of file ',filename])
48 disp('Recognized format: ')
49 disp('%%MatrixMarket matrix representation field symmetry')
50 error('Check header line.')
52 if ( ~ strcmp(head0,'%%MatrixMarket') )
53 error('Not a valid MatrixMarket header.')
55 if ( ~ strcmp(head1,'matrix') )
56 disp(['This seems to be a MatrixMarket ',head1,' file.']);
57 disp('This function only knows how to read MatrixMarket matrix files.');
62 % Read through comments, ignoring them
64 commentline = fgets(mmfile);
65 while length(commentline) > 0 & commentline(1) == '%',
66 commentline = fgets(mmfile);
69 % Read size information, then branch according to
70 % sparse or dense format
72 if ( strcmp(rep,'coordinate')) % read matrix given in sparse
73 % coordinate matrix format
75 [sizeinfo,count] = sscanf(commentline,'%d%d%d');
77 commentline = fgets(mmfile);
78 if (commentline == -1 )
79 error('End-of-file reached before size information was found.')
81 [sizeinfo,count] = sscanf(commentline,'%d%d%d');
82 if ( count > 0 & count ~= 3 )
83 error('Invalid size specification line.')
88 entries = sizeinfo(3);
90 if ( strcmp(field,'real') ) % real valued entries:
92 [T,count] = fscanf(mmfile,'%f',3);
93 T = [T; fscanf(mmfile,'%f')];
94 if ( size(T) ~= 3*entries )
96 str2mat('Data file does not contain expected amount of data.',...
97 'Check that number of data lines matches nonzero count.');
99 error('Invalid data.');
101 T = reshape(T,3,entries)';
102 A = sparse(T(:,1), T(:,2), T(:,3), rows , cols);
104 elseif ( strcmp(field,'complex')) % complex valued entries:
106 T = fscanf(mmfile,'%f',4);
107 T = [T; fscanf(mmfile,'%f')];
108 if ( size(T) ~= 4*entries )
110 str2mat('Data file does not contain expected amount of data.',...
111 'Check that number of data lines matches nonzero count.');
113 error('Invalid data.');
115 T = reshape(T,4,entries)';
116 A = sparse(T(:,1), T(:,2), T(:,3) + T(:,4)*sqrt(-1), rows , cols);
118 elseif ( strcmp(field,'pattern')) % pattern matrix (no values given):
120 T = fscanf(mmfile,'%f',2);
121 T = [T; fscanf(mmfile,'%f')];
122 if ( size(T) ~= 2*entries )
124 str2mat('Data file does not contain expected amount of data.',...
125 'Check that number of data lines matches nonzero count.');
127 error('Invalid data.');
129 T = reshape(T,2,entries)';
130 A = sparse(T(:,1), T(:,2), ones(entries,1) , rows , cols);
134 elseif ( strcmp(rep,'array') ) % read matrix given in dense
135 % array (column major) format
137 [sizeinfo,count] = sscanf(commentline,'%d%d');
139 commentline = fgets(mmfile);
140 if (commentline == -1 )
141 error('End-of-file reached before size information was found.')
143 [sizeinfo,count] = sscanf(commentline,'%d%d');
144 if ( count > 0 & count ~= 2 )
145 error('Invalid size specification line.')
151 if ( strcmp(field,'real') ) % real valued entries:
152 A = fscanf(mmfile,'%f',1);
153 A = [A; fscanf(mmfile,'%f')];
154 if ( strcmp(symm,'symmetric') | strcmp(symm,'hermitian') | strcmp(symm,'skew-symmetric') )
157 A = [A(1:currenti); zeros(j,1);A(currenti+1:length(A))];
159 elseif ( ~ strcmp(symm,'general') )
160 disp('Unrecognized symmetry')
162 disp('Recognized choices:')
165 disp(' skew-symmetric')
167 error('Check symmetry specification in header.');
169 A = reshape(A,rows,cols);
170 elseif ( strcmp(field,'complex')) % complx valued entries:
171 tmpr = fscanf(mmfile,'%f',1);
172 tmpi = fscanf(mmfile,'%f',1);
175 tmpr = fscanf(mmfile,'%f',1);
176 tmpi = fscanf(mmfile,'%f',1);
177 A = [A; tmpr + tmpi*i];
179 if ( strcmp(symm,'symmetric') | strcmp(symm,'hermitian') | strcmp(symm,'skew-symmetric') )
182 A = [A(1:currenti); zeros(j,1);A(currenti+1:length(A))];
184 elseif ( ~ strcmp(symm,'general') )
185 disp('Unrecognized symmetry')
187 disp('Recognized choices:')
190 disp(' skew-symmetric')
192 error('Check symmetry specification in header.');
194 A = reshape(A,rows,cols);
195 elseif ( strcmp(field,'pattern')) % pattern (makes no sense for dense)
196 disp('Matrix type:',field)
197 error('Pattern matrix type invalid for array storage format.');
198 else % Unknown matrix type
199 disp('Matrix type:',field)
200 error('Invalid matrix type specification. Check header against MM documentation.');
205 % If symmetric, skew-symmetric or Hermitian, duplicate lower
206 % triangular part and modify entries as appropriate:
209 if ( strcmp(symm,'symmetric') )
210 A = A + A.' - diag(diag(A));
212 elseif ( strcmp(symm,'hermitian') )
213 A = A + A' - diag(diag(A));
215 elseif ( strcmp(symm,'skew-symmetric') )