Using fopen for all .CSV files in a single folder

I am trying to read column C from all .CSV files in a single folder (which has data in columns A-E) and merge into one single column vector. I am using the following code:
w = 1;
z = [];
D = dir('C:\xxxxx\xxxx\xxx\xx\*.csv');
%
for k = 1:length(D)
fileID = (fopen(D(k,1).name,'r');
c = textscan(fileID,'%*d%*d%d%*d%*d', 'Delimiter', ',');
x = c{1,1};
fclose(fileID);
%
for n = 1:length(x)
z(w,1) = x(n);
w = w + 1;
end
end
There seems to be a problem with the fopen command as I am not able to read any values from the file.
When I test the code in the command prompt I am able to read values, but it is not working in the script with the loop.
I am not getting any errors, just not able to read any values.
Any ideas on what the problem is? Any suggestions? Thanks

 採用された回答

per isakson
per isakson 2015 年 5 月 18 日
編集済み: per isakson 2015 年 5 月 18 日

0 投票

Replace
fileID = (fopen(D(k,1).name,'r');
by
fileID = fopen( fullfile( folderspec, D(k,1).name), 'r' );
where
folderspec = 'C:\xxxxx\xxxx\xxx\xx';
and it will work - I've tested

2 件のコメント

Jacee Johnson
Jacee Johnson 2015 年 5 月 18 日
Thank you once again Per for your great contribution. I believe this is what I needed to get it to work. Unfortunately, I am going to sleep now but will test it first thing in the AM.
Thanks again.
Jacee Johnson
Jacee Johnson 2015 年 5 月 19 日
Yes this method worked, just as I expected. Thanks for all of your help!

サインインしてコメントする。

その他の回答 (2 件)

Ken Atwell
Ken Atwell 2015 年 5 月 19 日

1 投票

Also consider checking out datastore as a way to treat many files as a single data source.
Image Analyst
Image Analyst 2015 年 5 月 18 日

0 投票

Try changing 'Delimeter' to 'Delimiter'.

3 件のコメント

Jacee Johnson
Jacee Johnson 2015 年 5 月 18 日
Thanks for pointing that out and I wish that would have fixed the problem. However, that was just a typo when I transferred the code onto here.
Image Analyst
Image Analyst 2015 年 5 月 18 日
Why are you retyping code? You can do a copy and paste you know, which would avoid these errors. I doubt anybody will do anything more until you read this and attach a sample data file - otherwise it's just guessing, and I assume you'd prefer a tested and proven solution over a guess.
Jacee Johnson
Jacee Johnson 2015 年 5 月 18 日
Thanks for your help. I attached a sample .CVS file, maybe it will help. The rest of the code is correct.

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2015 年 5 月 18 日

回答済み:

2015 年 5 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by