Importing a CSV file as a nested cell array - is it possible
Mostrar comentarios más antiguos
I'm struggling to find any online help documentation on this . Is it possible to import a csv file derived from MSExcel into Matlab directly as a nested cell array
For example if the CSV file has several rows, each with a set of values such as
23,34,1
23,44,55,72,65
4,86,98,7,12,34,9
I want to import this as a nested cell array such that the main array is of size 3 x 1 and each cell contains the data in the csv file row as nested sub arrays. So cell (1,1)of the main array has a (1x3) subarray from the first row of the CSV. Cell 2 has a (1x5) subarray from the second row and Cell 3 has a (1x7) subarray from the third row of the CSV file.
When I use the standard import data function and select 'cell array' from the options Matlab just creates one large 3x 7 array in this example and puts NaNs in the blanks.
Is it actually possible to do this or do I need to create the nested array as a separate step after importing the CSV file and how do I do that .
Many thanks
Respuesta aceptada
Más respuestas (2)
Azzi Abdelmalek
el 12 de Jul. de 2015
Editada: Walter Roberson
el 13 de Jul. de 2015
0 votos
Import your data, then get ride of nan
1 comentario
AndyT
el 13 de Jul. de 2015
Walter Roberson
el 12 de Jul. de 2015
fid = fopen('YourFile.csv', 'rt');
counter = 0;
while true
inline = fgetl(fid);
if ~ischar(inline); break; end %end of file
counter = counter + 1;
datacell{counter} = textscan(inline, '%f', 'Delimiter', ',');
end
1 comentario
AndyT
el 13 de Jul. de 2015
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!