MATLAB: Use of “%” in exported file

general matlab usageMATLAB

hi there, i was wondering if there is an easy way to export a volumetric data of DTI (calculated tensor) in a particular format. Such as % Grid ————this row defines the coordinates of a 3D data % Data———–this section describes the data itself. The main issue so far is the use of “%” itself as i need it to define the column for coordinates and Data. I have tried “dlmwrite” function to first define coordinates in three consecutive rows and then by making use of offset option generated a gap between Grid and data and then i manually edited that portion. The issue of size for more practical volume data would definitely hinder me from opening such a gigantic file in any note editor package. So i was wondering if there is any possible way to include “%” at those particular locations in a file. Thanks

Best Answer

FID = fopen(FileName, 'w');
fprintf(FID, '%s\n', '% Comment line 1 ---');
fprintf(FID, [repmat('%g ', 1, 5), '\n'], transpose(rand(3, 5)));
% or:
fprintf(FID, '%%Comment line 2 ---');
fprintf(FID, [repmat('%g ', 1, 6), '\n'], transpose(rand(2, 6)));
fclose(FID);
The TRANSPOSE may be needed because the data are written in columnwise order.