You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: writeFDTD.m
    path of file: ./repos/HoloLib-MATLAB/source
    
    the code of the file until where you have to start completion: function writeFDTD(data, FILENAME)
%FDTD Solutions 仿真文件数据导出
%   writeFDTD(P, FILENAME) 将索引矩阵P写入到FILENAME文件

    
function writeFDTD(data, FILENAME)
%FDTD Solutions 仿真文件数据导出
%   writeFDTD(P, FILENAME) 将索引矩阵P写入到FILENAME文件
%   FILENAME文件将被FDTD Solutions仿真文件脚本作为数据输入
%   
%   data 是stepPhase函数生成的Index索引数据矩阵
%   FILENAME 是输出文件全名 应包含路径和文件名
%

if isa(data, 'uint8') == false
    error('不是索引矩阵');
end

% 生成随机文件名
tmpName = getRandomStr();
% 为加速存取速度先将数据存放在系统临时文件夹
fid = fopen([getenv('TMP') '\' tmpName '.tmp'], 'w');
[R, C] = size(data);
fprintf(fid, 'Index Value\n');
fprintf(fid, '%d %d\n', R, C);
i = 1;
for r = R:-1:1
	for c = 1:1:C
		fprintf(fid,'%d %d\n', i, data(r, c));
		i = i + 1;
	end