0

I have a folder where the post processed files from a numerical model are stored with different names. for e.g. - WL.{simulation_name}.nc, UVel.{simulation_name}.nc, etc.

I am writing a generalized script in MATLAB to pick out the files that I need and process it further, irrespective of the simulation name :

dirFiles = dir(pwd)

for ii = 3:size(dirFiles,1)
    s = dirFiles(ii).name
    if strfind(s,'WL')
       Data.WL = nc_varget(dirFiles(ii).name,'WL');
    end
end

The problem is the folder usually contains more than 30-40 files with different variables. And the loops in the above script are immensely slowing down the processing time.

Is there any way to have a cleaner and faster solution?

P.S: One way would be to use eval function, But I really don't want to use it unless it is the last option.

Thanks!! Cheers.

rk_x23
  • 45
  • 7
  • 1
    I really doubt that finding the correct file is the bottleneck. Your data import function `nc_target` is what makes you code slow. Is it written by you or a manufacturer? In the last case, the only thing you can and should do is **pre-allocating** your struct `Data.WL` somehow. It's hard to tell exactly how, without further knowledge your function. – Robert Seifert Feb 05 '15 at 12:11
  • nc_varget is just a tool to read the selected netCDF file, and usually is quite fast. But I'll check to see if that is the bottleneck. Thanks. :) – rk_x23 Feb 05 '15 at 12:20
  • @rk_x23, do you know about the [profiler](http://se.mathworks.com/help/matlab/ref/profile.html)? This is an excellent tool that matlab provides, which will show you exactly which parts of the code that takes the most time. Try it out! :) – patrik Feb 05 '15 at 13:49
  • Try `ls` rather than `dir`: possible duplicate of [Matlab dir() takes forever to run](http://stackoverflow.com/questions/17473925/matlab-dir-takes-forever-to-run) – horchler Feb 05 '15 at 15:54

1 Answers1

0

Replace 'dir' with 'rdir'

http://www.mathworks.com/matlabcentral/fileexchange/19550-recursive-directory-listing

Here you could shorten dirFiles, by using search patterns.

dirFiles = rdir([pwd '\*WL*.*')

BerndGit
  • 1,530
  • 3
  • 18
  • 47