MATLAB: Dimensions of matrices being concatenated… cell2mat

cell2mat errorMATLAB

Hello,
I got an error in cell2mat. Dimensions of matrices being concatenated are not consistent. Wtah is can be wrong?
data=cellfun(@str2num, array) does not help. Its giving 'NaN NaN NaN…'.
A code:
textFile = textscan(FileID, '%*s %*s %*s %*s %*s %s %s %s %s %s %s %s %s', 'Delimiter', '\t', 'headerlines', 54);
fclose(FileID);
clear FileID
for i = 1:size(textFile,2)
textFile{i} = strrep(textFile{i}, ',', '.');
textFile{i} = str2double(textFile{i});
end
clear i
m = cell2mat(textFile);
Signal = [m(:,1) m(:,3) m(:,4)];
clear textFile
clear m
textFile looks like:
66077×1 double 66077×1 double 66077×1 double 66077×1 double 66077×1 double 66077×1 double 66077×1 double 66076×1 double
and a first single cell:
0,00219999994442333
0,00619999984337483
0,0101999997423263
0,0141999996412778

Best Answer

"Wtah is can be wrong?"
The number of rows of all of the matrices are not the same. Take a look at the last one, it has one row fewer than the rest, so there is no way to horizontally concatenate those arrays:
66077x1 double
66077x1 double
66077x1 double
66077x1 double
66077x1 double
66077x1 double
66077x1 double
66076x1 double % how many rows?
You will have to ensure that they have the same number of rows, which might involve one of these:
  • fix the file itself.
  • fix your file importing code (e.g. detect missing values).
  • pad the last array.
  • trim the previous arrays.
I would recommend converting decimal commas to decimal point before using textscan (which happily accepts a string as its input), e.g. (pseudocode):
str = fileread(...)
str = strrep(str,',','.')
C = textscan(str,'%f%f%f...',...)
Related Question