Problem using cell2mat on cell arrays from textscan
조회 수: 4 (최근 30일)
이전 댓글 표시
I'm using textscan to import two columns of numbers from a csv file. The resulting array 'data' comes out as a cell array. I would prefer for the arrays to be numerical vectors instead, so I tried to use cell2mat on each of the data cell arrays, 'data{2}' and 'data{2}'.
fid = fopen(filename);
data = textscan(fid,'%s %s','Delimiter',',');
fclose(fid);
startTimes = cell2mat(data{1});
endTimes = cell2mat(data{2});
I end up with the error as shown below. I'm not sure what is causing this issue as the problem seems to be a dimension mismatch within the cell2mat function code.
Error using cat Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 84) m{n} = cat(1,c{:,n});
Error in respSpace_LoL_Analysis_v1 (line 19) startTimes = cell2mat(data{1});
댓글 수: 0
채택된 답변
Jiro Doke
2016년 12월 7일
편집: Jiro Doke
2016년 12월 7일
If they are numbers, why don't you read them in as a numeric (e.g. %f)?
data = textscan(fid,'%f %f','Delimiter',',');
Then, you can just extract out the cell components.
startTimes = data{1};
endTimes = data{2};
댓글 수: 1
Jiro Doke
2016년 12월 7일
The reason your code is failing is because the data was brought in as a character array, and the numbers had different number of digits. When you tried to cell2mat, it was trying to vertically concatenate the character arrays, which resulted in a dimension mismatch error.
추가 답변 (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!