Problem using cell2mat on cell arrays from textscan

4 Ansichten (letzte 30 Tage)
ScottPT303
ScottPT303 am 7 Dez. 2016
Kommentiert: Jiro Doke am 7 Dez. 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});

Akzeptierte Antwort

Jiro Doke
Jiro Doke am 7 Dez. 2016
Bearbeitet: Jiro Doke am 7 Dez. 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 Kommentar
Jiro Doke
Jiro Doke am 7 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by