Problem using cell2mat on cell arrays from textscan

2 次查看(过去 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});

采纳的回答

Jiro Doke
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
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 CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by