필터 지우기
필터 지우기

Importing a CSV file as a nested cell array - is it possible

조회 수: 3 (최근 30일)
AndyT
AndyT 2015년 7월 12일
편집: Stephen23 2015년 7월 13일
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

채택된 답변

Stephen23
Stephen23 2015년 7월 13일
편집: Stephen23 2015년 7월 13일
Here is a simple and neat solution using fileread and regexp:
out = regexp(fileread('temp.txt'),'[\n\r]+','split');
out = cellfun(@(s)sscanf(s,'%f,').', out, 'UniformOutput',false);
which we can check in the command window:
>> out{1}
ans =
23 34 1
>> out{2}
ans =
23 44 55 72 65
>> out{3}
ans =
4 86 98 7 12 34 9
The code was tested using this file:
  댓글 수: 2
AndyT
AndyT 2015년 7월 13일
Thank You Stephen , this does it and using function combinations I would never have found on my own. Much appreciated
Stephen23
Stephen23 2015년 7월 13일
편집: Stephen23 2015년 7월 13일
My pleasure! Note that it will only work for exactly that file format that you specified in your question: as soon as space characters or letters appear in there it will probably fail :)

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

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 12일
편집: Walter Roberson 2015년 7월 13일
Import your data, then get ride of nan
  댓글 수: 1
AndyT
AndyT 2015년 7월 13일
Thank you Azzi, not sure I understand this . If I import and get rid of NaN ( which I do with cellfun) it doesn't create the nested cells with the data in that I require.

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


Walter Roberson
Walter Roberson 2015년 7월 12일
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
AndyT
AndyT 2015년 7월 13일
Many thanks Walter, this works but seems to create 2 levels of the datacell array . The first is a 1x 1 cell and then that contains the data as another subcell e.g. datacell{1,1} is a 1x 7 cell . So the actual data is held in datacell{1,1}{1,1}.

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by