Efficiently Import and Organize Topographic Profile Data in Matlab

  • Context: MATLAB 
  • Thread starter Thread starter rocks rock
  • Start date Start date
  • Tags Tags
    Data files Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 8K views
rocks rock
Messages
3
Reaction score
0
Hi. I am trying to read csv files into Matlab. The files are x, y, z values for topographic profiles taken about 4 times a year over several years. I've worked out how to loop through each file and loop through names so that each comes in as a separate matrix. (see below) The problem I'm having is that some profiles are missing for certain years or seasons. So when Matlab comes to one of those files it says "Error File Not Found." I'm wondering if there is a way to skip over files that just don't exist.

Thanks

File naming scheme: prof_#_seasonyear
Ex: prof_3_sp08

Sample of code I'm using:

for i = 2:49
fname=sprintf('prof_%d_sp99.out',i);
eval(['sp99_prof_' num2str(i) '= csvread(fname);'])

fname=sprintf('prof_%d_sp00.out',i);
eval(['sp00_prof_' num2str(i) '= csvread(fname);'])

fname=sprintf('prof_%d_sp01.out',i);
eval(['sp01_prof_' num2str(i) '= csvread(fname);'])

end
 
Physics news on Phys.org
You could also use the dir or ls commands in MATLAB (once you've set the directory) to get a directory listing (I think both will work regardless of whether it's in a Windows / Linux / Mac environment):
http://www.mathworks.com/help/techdoc/ref/dir.html
http://www.mathworks.com/help/techdoc/ref/ls.html

You can also use this with a little more code to zero pack in place of missing values (assuming this is desirable). Or just insert zeros whenever you catch errors (as jhae2.718 suggests).