필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
papafos
papafos 2014년 4월 28일
댓글: Image Analyst 2014년 5월 4일
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!

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 28일
dd = dir('C:\Users\...\*.csv');
n=numel(dd);
for k=3:n;
filename=dd(k).name;
A{k}=importdata(filename);
...
end

Image Analyst
Image Analyst 2014년 4월 29일
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
papafos
papafos 2014년 5월 4일
편집: papafos 2014년 5월 4일
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.
Image Analyst
Image Analyst 2014년 5월 4일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by