Problem using cell2mat on cell arrays from textscan

4 visualizaciones (últimos 30 días)
ScottPT303
ScottPT303 el 7 de Dic. de 2016
Comentada: Jiro Doke el 7 de Dic. de 2016
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});

Respuesta aceptada

Jiro Doke
Jiro Doke el 7 de Dic. de 2016
Editada: Jiro Doke el 7 de Dic. de 2016
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 comentario
Jiro Doke
Jiro Doke el 7 de Dic. de 2016
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.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by