%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% EV script 014 - Export echogram images for all EV files in a folder.m
% Example Echoview COM script originally submitted to the Echoview forum by Carrie Wall (NOAA NGDC)
% Downloaded from www.echoview.com
% For assistance, contact Echoview support <support@echoview.com>
%
% This script exports echogram images for variable(s) in each Echoview file.
% - Enter the name of your variable between the quotation marks on line 19. 
% - Add more variables as needed here. If you do, remember to also add further commands after line 32. 
% - Be sure to change the path of the desired root directory
%
% This is a translation of the script "EV script 013 - Export echogram images for all EV files in a folder.vbs". 
% The output filename has been changed to include the timestamp range for the files in the fileset and the output image is not split into parts.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function batchFileToImage()

Variable = 'Angle select'; %%Change variable here
RootFolder = 'C:\Users\dmann\Documents\Echoview'; %%Change root directory here

EvFileFolder = uigetdir(RootFolder, 'Where are the EV files that you wish to use?');
EvFiles=dir(fullfile(EvFileFolder,'*.EV'));  
ExportFolder = uigetdir(RootFolder, 'Where would you like to save your exports?');
EvApp = actxserver('EchoviewCom.EvApplication');
 
for i=1:length(EvFiles);  
  EvFileName=fullfile(EvFileFolder,EvFiles.name);  
  EvFile = EvApp.OpenFile(EvFileName);
  if isempty(EvFile)
    msgbox('Failed to open EV file')             
  else
      EvVariable = EvFile.Variables.FindByName(Variable).AsVariableAcoustic(); 
      if isempty(EvVariable)
        msgbox('Failed to find variable')
      else
          Fileset = EvFile.Filesets.FindByName('Fileset1');
          ExportFileName = [ExportFolder '\' datestr(Fileset.StartTime,'yyyy-mm-dd HH-MM-SS') '_' datestr(Fileset.EndTime,'yyyy-mm-dd HH-MM-SS') '_' Variable '.png'];

          EvVariable.ExportEchogramToImage(ExportFileName,1080,-1,-1);
          EvFile.Save();
          disp('Done!')
      end
  end
  EvApp.CloseFile(EvFile);
end

end