% Write motion file
% This m file will export the complete ground reaction force data into a
% _grf.mot file

[m,n]=size(grf_complete);

new_file = strrep(txt_fname,'.txt','_grf.mot');
fid = fopen(new_file,'w');
% Print header

fprintf(fid,'name %s\n',trialname);
fprintf(fid,'datacolumns %d\n',n);
fprintf(fid,'datarows %d\n',m);
fprintf(fid,'range %f %f\n',time(1),time(end));
fprintf(fid,'endheader\n');
fprintf(fid,'time\tground_force_vx\tground_force_vy\tground_force_vz\tground_torque_x\tground_torque_y\tground_torque_z\t');
if n==19
    fprintf(fid,'ground_force_vx\tground_force_vy\tground_force_vz\tground_torque_x\tground_torque_y\tground_torque_z\t');
    fprintf(fid,'ground_force_px\tground_force_py\tground_force_pz\t');
end
fprintf(fid,'ground_force_px\tground_force_py\tground_force_pz\n');

% Print data
for j=1:m
    fprintf(fid,'%f\t',grf_complete(j,:));
    fprintf(fid,'\n');
end
fclose(fid);