import multiple csv files in matlab

3 ビュー (過去 30 日間)
Mar
Mar 2018 年 7 月 25 日
コメント済み: Mar 2018 年 7 月 26 日
I am trying to import several csv files and compact all of them in a single variable called "data".
The csv files are 2D images (files include only numbers - no headers. I want to make a stack of these 2D images and create a 3D volume. All images have the same x- and y-dimension (i.e. same number of columns and rows).
I came across with this post https://uk.mathworks.com/matlabcentral/answers/129127-how-do-i-import-csv-files-using-csvread
Below it's the code I used:
d=dir('/Users/Desktop/csv/2Dimage_*.clv');
n=length(d);
data=cell(1,n);
for i=1:n
data(i)=csvread(d(i).name);
end
data=cell2mat(data);
I tried to check the dimensions of the data but I don't get what I expected. I was expecting a 3D array but I got the following
ans =
0 0
Can someone help with this please?
  1 件のコメント
Guillaume
Guillaume 2018 年 7 月 25 日
編集済み: Guillaume 2018 年 7 月 25 日
The code you've written will not produce a 3D array, only a 2D array of images stacked horizontally. That is trivially fixed however.
More troubling is that your result would imply that data is completely empty and hence that nothing was read. How did you "check the dimensions of the data"?

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

採用された回答

Guillaume
Guillaume 2018 年 7 月 25 日
編集済み: Guillaume 2018 年 7 月 26 日
A simpler version of your code, that will produce a 3D array instead of stacking the images horizontally:
folder = ''/Users/Desktop/csv';
dircontent = dir(fullfile(folder, '2Dimage_*.csv');
images = arrayfun(@(f) csvread(fullfile(folder, f.name)), dircontent, 'UniformOutput', false);
image = cat(3, images{:}); %assumes all images are the same size. Otherwise will error
  6 件のコメント
Stephen23
Stephen23 2018 年 7 月 26 日
編集済み: Stephen23 2018 年 7 月 26 日
@Mar: download and use my FEX submission natsortfiles. Its online documentation, the HTML help, and Mfile help all contain plenty of examples. Probably you would put the names into a cell array:
names = natsortfiles({dircontent.name});
images = cellfun(@(name) csvread(fullfile(folder, name)), names, 'Uni',0);
Mar
Mar 2018 年 7 月 26 日
Thanks a lot!! All sorted now :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by