Unable to open .csv file ("Error using importdata (line 136) Unable to open file")

Hello!
I am using a loop to open a big number of .csv files. I have applied it to a folder with .csv files and it worked. However,I apply it now to another folder with .csv files and it doesn't work.
The loop is the following:
dd = dir('C:\Users\...\csv');
n=numel(dd);
for k=3:n;
filename=dd(k).name;
A=importdata(filename);
...
end
Any idea why it is not working? Thank you in advance!

Answers (2)

dd = dir('C:\Users\...\*.csv');
n=numel(dd);
for k=3:n;
filename=dd(k).name;
A{k}=importdata(filename);
...
end
If the only difference is the folder and the particular csv files, then perhaps one is corrupted . Can you attach the one that it bombs on?

8 Comments

I ran the loop again for different ranges inside this folder.In the specific folder I have 74 .csv files.The first 14 and the last 9 cannot be opened by the importdata inside the loop.The rest work fine. Image Analyst,I thought the same about "corrupted files",but the problem is only with the loop.I mean that if I try importdata for the specific files one by one it works.
That is not a normal csv file - it has at least two sets of data with a bunch of header lines separating the two. I don't think importdata is set up to handle concatenated files like that.
Yes,I know.However,I found a way to handle this type of files by using importdata() at the beginning and a combination of other functions later. My problem is still that importdata opens all of the files one by one,but cannot open some of them when I start a loop.
I don't understand. First you say that you have a way to handle it, then you say you don't , or at least it doesn't work when it's in a loop (which doesn't make sense).
Sorry,I will explain it better.There is a folder of 70 files.The loop that I posted above cannot run for the first 15 files and the last 10 files.If i limit the loop to the rest (from file 15 to file 60) it works fine. In addition,it is not possible the reason that some files not open because they are corrupted,because I can open these files with the importdata and the rest functions that I use,when I do this one-by-one (not with the loop). For the time being,because the files are not many,I used the loop for the files 15 to 60 and handled the rest with the same functions but one-by-one. However,because I have to do the same for foolder with much more .csv files,I wanted to find a solution and run the loop for all the files.
As you agree, it doesn't make sense. All I can do is to try it and try to debug it, so if you want, attach your 70 files and m-file in a zip file, and I'll see if I can try it and reproduce what you're seeing.
Finally,it worked for all the files with the following code.
dd = dir('C:\Users\...\csv');
n=numel(dd);
for k=3:n;
char1=dd(k).name;
char2='C:\Users\...\csv\';
filename=[char2 char1];
A=importdata(filename);
end
Thank you for the response.
Well what must have happened is that in the first case where it ran, you must have had the csv files in the current folder so you didn't need the folder name. In the second case the files were not in the current folder, where the m-file was, or even on the search path. So it didn't know where to find them. When you added
filename=[char2 char1];
now you told it the full filename and it could find them. The proper way, more robust way , to do this is:
folder = 'C:\Users\...\csv\';
if exist(folder, 'dir')
for k=3 : n
fullFileName= fullfile(folder, d(k).name);
if exist(fullFileName, 'file')
A=importdata(fullFileName);
end
end
else
errorMessage = sprintf('Error: folder does not exist:\n%s', folder);
uiwait(warndlg(errorMessage));
end
I suggest you make these changes to improve your code.

Sign in to comment.

Categories

Asked:

on 28 Apr 2014

Commented:

on 4 May 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!