Jan 6, 2010

A Matlab file saving format

Let me take this as a good start.
There is one special requirement for saving Matlab vectors into a file with certain format: updated vector Vec in each step should be saved like,
[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 6, 7]
......
Which mean each vector is enclosed with [ and ], each entry separated by a comma. No comma after the last entry.
There could be more special requirements for the file format. Other commands' combination can accomplish more complicated task, such as "dlmwrite", "csvwrite".

A snippet Matlab code is:

fid = fopen('Hdata.dat','wb');
% Inside a loop body
% vec is updated
fprintf(fid, '[');
fprintf(fid,'%12.8f,', vec); % add a comma after each entry
fseek(fid,-1,0); % file pointer rewind one
fprintf(fid, ']\n'); % cover the last comma with a ]
% end of a loop body

fclose(fid);

No comments: