How can I load a big table in Matlab using textscan
7 views (last 30 days)
Show older comments
I am trying to load a csv file on matlab using textscan. C is a 1x4 cell array with each element being a 36x1 cell. I try to get the table by using the cell2table function but returns a 1x4 table. I need a 36x4 table but I am not sure why the cell doesnt load as a 36X4 cell in the first place.
%
f = fopen('test.csv');
formataux = '%s%f%f%s';
C =textscan(f, formataux,'Headerlines', 1, 'Delimiter', ',', 'EmptyValue', NaN,'CollectOutput',1); % collect all out put. no need to use struct as I dont have to manipulate each column
fclose(f);
data = cell2table(C);
3 Comments
Stephen23
on 4 Mar 2019
Edited: Stephen23
on 4 Mar 2019
"is there a better way?"
readtable
Or if memory is a resctricting factor, you could use tall arrays:
"First column is the stock name (string), 2nd and thrid are open and close price (double ) and the 4th is the stock exchange."
Sure. But did you actually look at the class of the cells 2 and 3 contents, like I told you to? Are they really cell arrays (as your wrote in your question) ? (hint: no)
When you read the cell2table documentation it clearly states "...converts the contents of an m-by-n cell array, C, to an m-by-n table...". Do you have an mxn cell array? (hint: yes, you have a 1xn cell array, and that is the size table that you will get, exactly as the documentation states).
"but I am not sure why the cell doesnt load as a 36X4 cell in the first place."
textscan returns a 1xn cell array, each cell of which can be a column vector or matrix (usually numeric or cell array of char vectors). It does not return an nxm cell array (with n>1).
Either pay particular attention to sizes and classes, or use readtable.
Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!