import multiple csv files in matlab

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 comentario

Guillaume
Guillaume el 25 de Jul. de 2018
Editada: Guillaume el 25 de Jul. de 2018
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"?

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 25 de Jul. de 2018
Editada: Guillaume el 26 de Jul. de 2018
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 comentarios

Mar
Mar el 25 de Jul. de 2018
I still have the same problem. One thing that I've noticed is that my files have the extension .csv, i.e. 2Dimage_1.csv, 2Dimage_1.csv etc etc. I think the problem starts from the very first line when I am trying to import the data. When I tried to test the size of dircontent I got ans = 0 1
I changed the first line to dircontent = dir('/Users/Desktop/csv/2Dimage_*.csv'); and I got ans = 19 1. Although it's reading the data this way I got the error:
Error using csvread (line 35) File not found.
Error in mult_csv>@(f)csvread(f.name)
Error in mult_csv (line 11) images = arrayfun(@(f) csvread(f.name), dircontent, 'UniformOutput', false);
Guillaume
Guillaume el 26 de Jul. de 2018
Well, looking for files with extension clv or files with extension csv is of course not going to return the same set of files.
The error is because csvread needs to look for the files in the correct folder rather than the current one. I overlooked that. It's easily fixed. Answer updated.
Mar
Mar el 26 de Jul. de 2018
Editada: Mar el 26 de Jul. de 2018
Great, now it works fine! Thank you a lot for your help :)
Mar
Mar el 26 de Jul. de 2018
OK, so there is a problem still. The data import fine, are stack all together in the array image but are not in the right order. Seems they are allocated randomly. How can I make them stuck in ascending order? i.e. 0,1,2,3, etc
K>> dircontent.name
ans =
'file_0.csv'
ans =
'file_1.csv'
ans =
'file_10.csv'
ans =
'file_11.csv'
ans =
'file_12.csv'
ans =
'file_13.csv'
ans =
'file_14.csv'
ans =
'file_15.csv'
ans =
'file_16.csv'
ans =
'file_17.csv'
ans =
'file_18.csv'
ans =
'file_19.csv'
ans =
'file_2.csv'
ans =
'file_3.csv'
ans =
'file_4.csv'
ans =
'file_5.csv'
ans =
'file_6.csv'
ans =
'file_7.csv'
ans =
'file_8.csv'
ans =
'file_9.csv'
Stephen23
Stephen23 el 26 de Jul. de 2018
Editada: Stephen23 el 26 de Jul. de 2018
@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 el 26 de Jul. de 2018
Thanks a lot!! All sorted now :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

Mar
el 25 de Jul. de 2018

Comentada:

Mar
el 26 de Jul. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by