MATLAB: Loop to read netcdf files

loopnetcdf

hi, I have hundreds of netcdf files to work and I want to generate a loop to read all of them and save the result in other folder. The result it is a cut (a smaller region of interest) and then I will calculate the mean value. I have done the code for a single file and it works:
ncid1=netcdf.open('ESACCI-SOILMOISTURE-L3S-SSMV-COMBINED-20100101000000-fv02.2.nc', 'NC_NOWRITE');varname = netcdf.inqVar(ncid1,4);varid = netcdf.inqVarID(ncid1,varname);data = netcdf.getVar(ncid1,varid);lon1=netcdf.getVar(ncid1,0,0,1440);lat1=netcdf.getVar(ncid1,1,0,720);[longrid,latgrid]=meshgrid(lon1,lat1);data2=data((longrid>-1)&(longrid<0)&(latgrid>39)&(latgrid<40));
(the mean value I will calculate it when I have all the files cut). I have found some information on previous answers but I am still not able to solve it. It works until sentence "%Now do whatever you want with this file name" because it is copied from internet and I don't know how to continue:
% Specify the folder where the files are.myFolder = 'G:\DATA_validation\ESA\data\daily_files\COMBINED\v02.2\2010\files';% Check to make sure that folder actually exists.  Warn user if it doesn't.if ~isdir(myFolder)  errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);  uiwait(warndlg(errorMessage));  return;end% Get a list of all files in the folder with the desired file name pattern.filePattern = fullfile(myFolder, '*.nc'); theFiles = dir(filePattern);for k = 1 : length(theFiles)  baseFileName = theFiles(k).name;  fullFileName = fullfile(myFolder, baseFileName);  fprintf(1, 'Now reading %s\n', fullFileName);    % Now do whatever you want with this file name    ncid=netcdf.open(theFiles(k), 'NC_NOWRITE');    varname = netcdf.inqVar(ncid,4);    varid = netcdf.inqVarID(ncid,varname);    data(k) = netcdf.getVar(ncid,varid);    end
Thanks in advanced, I'm not good coding… if you don't understand me just ask!

% Specify the folder where the files are.myFolder = 'G:\SWICCA\DATA_validation\ESA\data\daily_files\COMBINED\v02.2\2010\files';% Check to make sure that folder actually exists.  Warn user if it doesn't.if ~isdir(myFolder)  errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);  uiwait(warndlg(errorMessage));  return;endfilePattern = fullfile(myFolder, '*.nc'); theFiles = dir(filePattern);% Loop for each nc-filefor i = 1:length(theFiles)  baseFileName = theFiles(i).name;  fullFileName = fullfile(myFolder, baseFileName);%     fprintf(1, 'Now reading %s\n', fullFileName);%     ncdisp(ncfiles(i).name) ;     ncid=netcdf.open(fullFileName, 'NC_NOWRITE');     varname= netcdf.inqVar(ncid,4);     varid = netcdf.inqVarID(ncid,varname);%      data = netcdf.getVar(ncid,varid);     netcdf.close(ncid);end