function MATLAB2UMDES(filename,FSA,S)
%MATLAB2UMDES(filename,FSA,S)
%INPUTS:
%filename = name of existing text file to be overwritten with FSA data
%FSA = name of matlab FSA
%S = state names (optional)

ns = length(FSA);
if nargin == 2
    S = cell(ns,1);
    for i = 1:ns
        S{i} = num2str(i);
    end 
end
fid = fopen(filename,'w');
fprintf(fid,num2str(ns));
fprintf(fid,'\n');
fprintf(fid,'\n');
C = {'uc' 'c'};
O = {'uo' 'o'};
for i = 1:ns
    ne = length(FSA(i).events);
    fprintf(fid,S{i});
    fprintf(fid,'\t');
    fprintf(fid,'0\t');
    fprintf(fid,num2str(ne));
    fprintf(fid,'\n');
    for j = 1:ne
        fprintf(fid,FSA(i).events{j});
        fprintf(fid,'\t');
        fprintf(fid,S{FSA(i).ter(j)});
        fprintf(fid,'\t');
        fprintf(fid,C{FSA(i).con(j)+1});
        fprintf(fid,'\t');
        fprintf(fid,O{FSA(i).obs(j)+1});
        fprintf(fid,'\n');
    end
    fprintf(fid,'\n');
end
fclose(fid);