How can I load a big table in Matlab using textscan
조회 수: 7 (최근 30일)
이전 댓글 표시
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
Stephen23
2019년 3월 4일
편집: Stephen23
2019년 3월 4일
"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.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!