restructure cell array from textscan
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi folks, I'm using textscan to bring in data from a .csv file to turn it into a cell array. The cell array consists of one row with multiple columns, each containing one row with its own {M x N} cell in it. (i.e., each column in my .csv dataset gets compressed into its own cell).
C =
Columns 1 through 3
{5x1 cell} {5x1 cell} {5x1 cell}
Is there a way to instead generate a cell array with a single column and multiple rows, each row containing one row of data from my .csv file? (i.e., each row in my .csv dataset gets compressed into its own cell). Tying to get it to look like this (if its even possible):
C =
{3x1 cell}
{3x1 cell}
{3x1 cell}
{3x1 cell}
{3x1 cell}
Reason being is I'm bringing in a large number of very large .csv files, concatenating them and exporting as a tab-delimited .txt file using fprintf. Due to memory limitations, I can only import one file at a time to write to the output file.
Thanks!
댓글 수: 3
Image Analyst
2018년 8월 3일
Why not just simply use csvread() to read the data into a double array? Why hassle with the complications of a cell array???
채택된 답변
Jan
2018년 8월 4일
Some testdata:
C = {};
for i1 = 1:3
for i2 = 1:5
C{i1}{i2} = i1*10+i2;
end
end
Now the conversion:
CC = num2cell(cat(1, C{:}), 1)
Maybe you want to transpose CC. But is this really useful?
Reason being is I'm bringing in a large number of very large .csv files,
concatenating them and exporting as a tab-delimited .txt file using fprintf.
It seems to be much easier to import the files as text, use strrep to replace the commas by tabs and append the result as string. The conversion and reshaping of cells is most likely a waste of time.
댓글 수: 3
Jan
2018년 8월 5일
편집: Jan
2018년 8월 5일
fprintf does not have a "Delimiter" argument.
What about:
Str = fileread(FileName);
Str = strrep(Str, ',', sprintf('\t'));
Str = strrep(Str, ' ', ''); % remove spaces?
fid = fopen(OutputFile);
fwrite(fid, Str, 'char');
fclose(fid);
This replaces all commas by tabs. But is this useful at all? What do you want to achieve actually? Which transformation is wanted? I have the impression that converting the output of textscan is a confusion indirection only.
추가 답변 (1개)
jonas
2018년 8월 4일
편집: jonas
2018년 8월 4일
What about this solution? I've replaced one of the numbers in the first column with a string, just to make sure it works.
%%Read data
opts = detectImportOptions('example_data.csv','NumHeaderLines',6);
opts = setvartype(opts,'double')
T=readtable('example_data.csv',opts);
%%Save 15 columns only
T=T{:,1:15};
%%Save to cell array if you really want
B=mat2cell(T,15,ones(1,15))'
B =
15×1 cell array
[15×1 double]
[15×1 double]
[15×1 double]
...
Strings are stored as NaN, except if the string is Inf, in which case it is stored as Inf. If you, for some reason want strings instead of doubles, then change the argument of setvartype to 'string'.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!