Problem using cell2mat on cell arrays from textscan

4 visualizzazioni (ultimi 30 giorni)
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});

Risposta accettata

Jiro Doke
Jiro Doke il 7 Dic 2016
Modificato: Jiro Doke il 7 Dic 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 Commento
Jiro Doke
Jiro Doke il 7 Dic 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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by