restructure cell array from textscan
显示 更早的评论
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???
Peter Lobato
2018-8-4
采纳的回答
更多回答(1 个)
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'.
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!