How can I load a big table in Matlab using textscan

7 views (last 30 days)
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);
Screenshot 2019-03-04 at 17.00.50.png
  3 Comments
Nikolaos Vasilas
Nikolaos Vasilas on 4 Mar 2019
Hi thanks for you reply. This is just an example Im using to illustrate the task. The actual csv I want to load has 12 million rows and is 1GB long so I thought that text scan should be more efficient. is there a better way?
First column is the stock name (string), 2nd and thrid are open and close price (double ) and the 4th is the stock exchange. I just need to load this in an actual table format, where its column is of different file type.
Stephen23
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.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!