MATLAB Matlab read in data files

  • Thread starter rocks rock
  • Start date
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
 

MATLABdude

Science Advisor
1,646
4
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).
 

Related Threads for: Matlab read in data files

Replies
4
Views
12K
Replies
3
Views
2K
Replies
5
Views
6K
Replies
5
Views
8K
Replies
3
Views
2K
Replies
1
Views
4K
Replies
1
Views
12K
Replies
4
Views
5K

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top