function SaveStrangeFormat(filename, STIM)

% function SaveStrangeFormat(filename, STIM)
%
% reads text file and display it to the console
%
% INPUT : filename = a string containing the file name
% : STIM = a vector of struct cointaining the following fields
% : nStim = the stimulus number
% : MPos = a Matrix 2 x Npos of mouse clicks
% : time = a vector 1 x Npos of time for each click
% Author: Borgo, Soranzo, Grassi 2010

try
    if ~exist(filename) % control if the file doesn't exist
        fid=fopen(filename,'w'); % open file to write
        if fid == -1 % it can be not possible
            disp(sprintf('Unable to write file ''%s'' ',filename));
        else % in this case file opens correctly
            for i=1:length(STIM) % write data loop
                fprintf(fid,'Stimolous number: %d \n',STIM(i).nStim);
                fprintf(fid,'\t Time (ms) \t Mouse Position\n');
                for j=1:length(STIM(i).time)
                    fprintf(fid,'\t %4d \t \t x=%4.0f y=%4.0f \n',...
                        STIM(i).time(j),STIM(i).MPos(j,1),STIM(i).MPos(j,2));
                end
            end
            fclose(fid); % close the file
            disp('File saved correctly!');
        end
    else
        disp('The file already exists, please change file name');
    end
catch
    fclose(fid);
    disp('Something wrong happened');
end